Button size relative to parent size in XML layout?

Is it possible to set the button size in the XML layout file so that, on the one hand, it does not occupy the entire width of the parent (screen), and on the other hand, does not use absolute pixel values?

To better explain my question, the problem in the following snippet is that "match_parent" accepts the entire width:

 <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="16px" android:paddingLeft="16px" android:paddingRight="16px" android:paddingTop="16px" android:text="@string/first_button" /> 

I know that you can control the size at runtime, but I'm interested in knowing if this is possible in the XML file.

Is there something like android:layout_width="match_HALF_parent" ?

+4
source share
3 answers

A raw example might be:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:text="Button" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="100dp" android:layout_marginTop="100dp" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" ></Button> </LinearLayout> 

Alternatively, you can install android:padding in your layout instead of the field on the button.

+2
source

Layout layouts can do this. Basically you need to set weightSum in the parent layout, and then set layout_weight in the womb:

 <?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:weightSum="2" android:orientation="horizontal"> <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:paddingBottom="16px" android:paddingLeft="16px" android:paddingRight="16px" android:paddingTop="16px" android:text="@string/first_button" /> </LinearLayout> 

This relationship between the parental weight Sum and the child's weight determines the width of the child: in this case, it will be half; if we increased the amount of parental weight to 3, then the child would be one third the width of his parent.

I wrote about this on the blog .

+6
source

The solution is to enclose the harsh LinearLayouts. Usa has one vertical LinearLayout as a container and a stack of horizontal LinerarLayouts on it. Each horizontal with weight:

 android:layout_weight="0.5" 

You should also set the width and height as follows:

 android:layout_width="match_parent" android:layout_height="match_parent" 

Finally, you fill out these horizontal layouts with widgets (like buttons) and give them weight.

The source might look like the following snippet:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:background="@drawable/bg" tools:context=".MainActivity"> <!--parent container--> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--child container--> <!--each horizontal layout gets 1/3 of remaining space--> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_weight="0.5" android:layout_height="match_parent"> <Button android:background="@color/colorPrimary" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="fill_parent" android:text="100%" android:textSize="60dp"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_weight="0.5" android:layout_height="match_parent"> <Button android:background="@color/colorPrimary" android:layout_width="fill_parent" android:layout_weight="0.5" android:layout_height="fill_parent" android:text="50%" android:textSize="60dp"/> <Button android:background="@color/colorPrimaryDark" android:layout_width="fill_parent" android:layout_weight="0.5" android:layout_height="fill_parent" android:textSize="60dp" android:text="50%"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_weight="0.5" android:layout_height="match_parent"> <Button android:background="@color/colorPrimary" android:layout_width="fill_parent" android:layout_weight="0.7" android:layout_height="fill_parent" android:text="30%" android:textSize="60dp"/> <Button android:background="@color/colorPrimaryDark" android:layout_width="fill_parent" android:layout_weight="0.3" android:layout_height="fill_parent" android:allowUndo="false" android:text="70%" android:textSize="60dp"/> </LinearLayout> <!-- Just for spacing--> <RelativeLayout android:layout_width="match_parent" android:layout_height="20dp"> </RelativeLayout> </LinearLayout> </LinearLayout> 

To get this as a result:

LinearLayout Stack

0
source

All Articles