android - Defining a percentage width for a LinearLayout? -
i want define percentage width (70%) linearlayout contains buttons, can center , child buttons can fill_parent. here's picture showing mean:
my current layout looks this:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layoutcontainer" android:orientation="vertical"> <linearlayout android:layout_width="fill_parent" android:id="@+id/barcontainer" android:orientation="horizontal" android:layout_height="40dp" android:background="@drawable/titlebackground"> <imageview android:id="@+id/barlogo" android:src="@drawable/titlelogo" android:layout_gravity="center_vertical" android:adjustviewbounds="true" android:layout_height="25dp" android:layout_width="wrap_content" android:scaletype="fitxy" android:paddingleft="5dp"></imageview> </linearlayout> <textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center_horizontal" android:id="@+id/searchtip" android:text="@string/searchtip" android:paddingtop="10dp" android:paddingbottom="10dp"></textview> <linearlayout android:layout_height="wrap_content" android:id="@+id/linearlayout1" android:orientation="vertical" android:layout_width="wrap_content"> <button android:text="button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> <button android:layout_width="wrap_content" android:id="@+id/button2" android:layout_height="wrap_content" android:text="button"></button> <button android:layout_width="wrap_content" android:id="@+id/button3" android:layout_height="wrap_content" android:text="button"></button> </linearlayout> </linearlayout>
the linearlayout im referring has id: linearlayout1. how do this?
you have set weight property of elements. create 3 relativelayouts children linearlayout , set weights 0.15, 0.70, 0.15. add buttons second relativelayout(the 1 weight 0.70).
like this:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layoutcontainer" android:orientation="horizontal"> <relativelayout android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="0.15"> </relativelayout> <relativelayout android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="0.7"> <!-- part that's 70% of total width. i'm inserting linearlayout , buttons.--> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerinparent="true" android:orientation="vertical"> <button android:text="button1" android:layout_width="fill_parent" android:layout_height="wrap_content"> </button> <button android:text="button2" android:layout_width="fill_parent" android:layout_height="wrap_content"> </button> <button android:text="button3" android:layout_width="fill_parent" android:layout_height="wrap_content"> </button> </linearlayout> <!-- 70% width end--> </relativelayout> <relativelayout android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="0.15"> </relativelayout> </linearlayout>
why weights 0.15, 0.7 , 0.15? because total weight 1 , 0.7 70% of total.
result:
edit: @simonveloper pointing out orientation should horizontal , not vertical , @andrew pointing out weights can decimals instead of integers.
Comments
Post a Comment