Trying to press a button on the bottom of a line layout in Android

I know that this is very easy to do using the Relative Layout, but I feel that I am doing the right thing and should give me the desired result with the most linear layout. But for some reason, when I run this on Google Nexus 7 running Android JB 4.1.2, I see a button and text view right after the list items. If the list view is empty, I see them at the top of the screen. Am I doing something wrong ?? Here is my xml file

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:orientation="vertical"> <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/delete" android:textSize="22sp" /> <TextView android:text="@string/attach_warning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout> 
+6
source share
5 answers

If you want to center an element in the middle of the screen, do not use LinearLayout, since they are designed to display several elements in a row laid one after another.

Use RelativeLayout .

So replace:

 android:layout_gravity="center" 

for the corresponding RelativeLayout option:

 android:layout_centerInParent="true" 
0
source
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="1" > <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="70dp" android:layout_weight="0.9" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_weight="0.1" android:gravity="bottom" > <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="delete" android:textSize="22sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="attach_warning" android:textSize="18sp" /> </LinearLayout> </LinearLayout> 
+1
source

Shriman, please take a look at this answer, and then tell me if this is helpful.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom" android:orientation="vertical" android:weightSum="1" > <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="delete" android:textSize="22sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="attach_warning" android:textSize="18sp" /> </LinearLayout> </LinearLayout> 
+1
source

change your xml code.

using android:layout_weight to adjust the percentage of your layout.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:orientation="vertical"> <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="0.8" /> <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight="0.1" android:text="@string/delete" android:textSize="22sp" /> <TextView android:text="@string/attach_warning" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="0.1" android:textSize="18sp" /> </LinearLayout> 
0
source

I know that this question already has an accepted answer, but I will still answer, since no solution has been found, and this is the first result that Google gave me about this problem, which I also had.

Just put LinearLayout (vertical) with layout:weight = 1 between the two elements you want to split.

In your code it will be

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/attachments_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:orientation="vertical"> <ListView android:id="@+id/mtg_attachments" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="0" /> <LinearLayout <!-- You should add this one and remove android:layout_weigth="1" form the listview --> android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> </LinearLayout> <Button android:id="@+id/attach_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/delete" android:textSize="22sp" /> <TextView android:text="@string/attach_warning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout> 

Please note that the added LinearLayout must be the only one with the set weight, otherwise we will have to deal with some problem.

0
source

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


All Articles