Cómo hacer que la ubicación de un botón sea móvil según la cantidad de elementos en ListView
Frecuentes
Visto 382 equipos
1
In a layout file I have a Listview whose size can grow/shrink dynamically. I have a button btn_rec_add and it's click event I add an item in the ListView. I have tried many changes in the Layout file but haven't been able to make the button shift its location based on number of items in the ListView. If I keep the button in the same RelativeLayout which has the ListView, then the button moves dynamically which is exactly how I want but I can't see the button after adding 5 or more elements in 4.3 inch display phones. If I keep the button outside the RelativeLayout of the ListView, then it is fixed on the screen.
Currently, the btn_rec_add is fixed to the bottom of the layout. Can someone please help me solve this problem.
Aquí está el código XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg" >
<ImageView
android:id="@id/top_bar_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/top_bar"
android:contentDescription="@string/content" />
<TextView
android:id="@+id/txt_recipients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:padding="8dp"
android:text="@string/text_recipients"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<ImageButton
android:id="@id/btn_back"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@string/content"
android:paddingTop="6dp"
android:src="@drawable/ic_back" />
<RelativeLayout
android:id="@+id/Rlayout_recipients"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/btn_rec_add"
android:layout_alignParentLeft="true"
android:layout_below="@id/top_bar_view" >
<ListView
android:id="@+id/rec_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="0dp"
android:paddingTop="20dp" />
</RelativeLayout>
<ImageButton
android:id="@+id/btn_rec_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/content"
android:src="@drawable/icon_add" />
</RelativeLayout>
2 Respuestas
0
If I understand correctly, you want the behavior of the button to be as follows:
- Appear below the last
ListView
item if theListView
does not extend to fill screen - Si
ListView
extends the full height of the screen, the button should be at the bottom of the screen, but the list should remain scrollable
If my understanding is correct, you should place your ListView
and your button in a LinearLayout
como sigue:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="@dimen/button_height"
android:background="@drawable/button_image" />
</LinearLayout>
The effect of the above layout is as follows:
- Layout in which items are vertically placed
- Layout which will be as wide as parent, but as tall as
ListView
yButton
ListView
will take up all of the space in the layout that the button does not occupy (this islayout_weight="1"
mientrasButton
has no layout weight so it will simply fill as much space as it needs as defined in this case by@dimen/button_height
)
Great Android layout question!
Respondido 12 Feb 14, 08:02
-1
Your Problem is related to User Experience. You have to decide whether user will like to scroll to end of the list to press add button or user want to add without scrolling to end of list. Since you only have two options with our scenario, either keep add button fixed or add it as footer of listview.
Respondido 12 Feb 14, 07:02
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android xml android-listview android-linearlayout android-relativelayout or haz tu propia pregunta.
Make sure you understand what is going on with the
LinearLayout
. They are really useful layouts! If you learned from this in addition to solving your issue, go ahead and upvote it as well: meta.stackexchange.com/a/198551/187616 - Daniel Smith