Button below a list not showing
I finally managed to get my list to display properly (http://stackoverflow.com/questions/5662277/how-to-line-up-intger-output-in-custom-android-dialog).
Now I am trying to add a button below the list. What happens is that when the list is too big the button doesn't show. The list is scrollable but that's it. Also, the scrollbar covers some of the data.
This is the main XML file:
<?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">
<LinearLayout android:id="@+id/lloPlayerNames" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/tvwPlayer1Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer2Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer3Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer4Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_margin="2dp"/>
</LinearLayout>
<ListView android:id="@+id/lvwScores" android:layout_below="@id/lloPlayerNames"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<Button android:id="@+id/btnOK "
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_below="@id/lvwScores" android:layout_weight="1"
android:text="OK" android:layout_centerHorizontal="true" />
</RelativeLayout>
This is the one for the rows:
<?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="wrap_content" android:orientation="horizontal">
<TextView android:id="@+id/tvwPlayer1Score" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer2Score" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer3Score" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer4Score" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
I tried changing the main to a linear layout but that made it even worse! The list looked like it had been on a crash-diet and was super-thin.
Tried:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/lloPlayerNames" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/tvwPlayer1Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer2Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer3Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer4Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginRight="2dp"/>
</LinearLayout>
<Button android:id="@+id/btnOK"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="OK" android:layout_centerHorizontal="true"
layout_alignParentBottom="true"/>
<ListView android:id="@+id/lvwScores" android:layout_above="@id/btnOK" android:layout_below="@id/lloPlayerNames"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_weight="1"/>
</RelativeLayout>
and what I get is:
when I try:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">>
<LinearLayout android:id="@+id/lloPlayerNames" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/tvwPlayer1Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer2Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer3Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/tvwPlayer4Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginRight="2d开发者_如何学JAVAp"/>
</LinearLayout>
<ListView android:id="@+id/lvwScores"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_weight="1"/>
<Button android:id="@+id/btnOK"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="OK" android:layout_centerHorizontal="true"
layout_alignParentBottom="true"/>
</LinearLayout>
I get:
Following is what I do on a couple of my apps with a ListView or a ScrollView, where I also have a fixed button or LinearLayout of buttons at the bottom. The "magic" is to set android:layout_weight="1"
in the ListView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fastScrollEnabled="true" />
<TextView
android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Quotes!" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="My Button" />
</LinearLayout>
You should put the button before the list and then use the layout_above property on the list element to position it above the button. Also add layout_alignParentBottom="true"
to your button.
Like so:
<?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">
<LinearLayout
android:id="@+id/lloPlayerNames" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tvwPlayer1Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvwPlayer2Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvwPlayer3Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvwPlayer4Name" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_margin="2dp"/>
</LinearLayout>
<Button android:id="@+id/btnOK"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_below="@id/lvwScores" android:layout_weight="1"
android:text="OK" android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
<ListView
android:id="@+id/lvwScores"
android:layout_below="@id/lloPlayerNames"
android:layout_above="@id/btnOK"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
Try adding a attribute android:below="@id/lvwScores"
to the Button
in main xml, this will place the Button
below the ListView
.
See this tutorial for more about RelativeLayout
.
精彩评论