How to place buttons and spinners with linearlayout?

I have this code:

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="85dp" android:ems="10" android:inputType="textPostalAddress" android:text="neshto"> <requestFocus /> </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <Button android:id="@+id/buttonOk" android:layout_width="75dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="0.5" android:text="Ok" /> <Button android:id="@+id/buttonCancel" android:layout_width="75dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="0.5" android:text="Cancel" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > </LinearLayout> <Spinner android:id="@+id/size" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/color" android:layout_width="match_parent" android:layout_height="wrap_content"/> 

I can’t post the image with the result I'm looking for, but I want the two OK and Cancel buttons to be at the bottom, and the two spinners standing almost directly to the text box. But I can not handle these layouts. Can someone give me an idea? I have tried this many times, but it still does not look the way I want.

+4
source share
2 answers

Use RelativeLayout instead of LinearLayout as a layout container:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="85dp" android:ems="10" android:inputType="textPostalAddress" android:text="neshto" > <requestFocus /> </EditText> <Spinner android:layout_below="@+id/editText" android:id="@+id/size" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Spinner android:layout_below="@+id/size" android:id="@+id/color" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <Button android:id="@+id/buttonOk" android:layout_width="75dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="0.5" android:text="Ok" /> <Button android:id="@+id/buttonCancel" android:layout_width="75dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="0.5" android:text="Cancel" /> </LinearLayout> </RelativeLayout> 
-1
source

Try the following:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="85dp" android:ems="10" android:inputType="textPostalAddress" android:text="neshto" > <requestFocus /> </EditText> <Spinner android:id="@+id/size" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/color" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <View android:id="@+id/strut" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerHorizontal="true" /> <Button android:id="@+id/buttonOk" android:layout_width="75dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_gravity="center_horizontal" android:layout_toLeftOf="@id/strut" android:layout_weight="0.5" android:text="Ok" /> <Button android:id="@+id/buttonCancel" android:layout_width="75dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_gravity="center_horizontal" android:layout_toRightOf="@id/strut" android:layout_weight="0.5" android:text="Cancel" /> </RelativeLayout> </LinearLayout> 
+2
source

Source: https://habr.com/ru/post/1490643/


All Articles