Tuesday, May 8, 2012

How to set View width (height) in percentage of parent View in Android


How to set View width (height) in percentage of parent View in Android



Example:
I want to set child LinearLayout height = 50% of parrent LinearLayout in Android. Like in HTML.
To set height in percents I do some trick with android:layout_weight and android:weightSum
Here the screenshot:
Android Layout height equals 50 percents of parrent


To do this you need do somethind like this. Look at code below:





<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:weightSum="100">
 <LinearLayout android:layout_weight="50"
  android:layout_width="match_parent"
  android:id="@+id/linearLayout1"
  android:layout_height="wrap_content"
  android:orientation="vertical"></LinearLayout>
</LinearLayout>

No comments: