Limit height of ListView on Android

后端 未结 16 1327
广开言路
广开言路 2020-11-28 23:07

I want to show a button under a ListView. Problem is, if the ListView gets extended (items added...), the button is pushed out of the screen.

相关标签:
16条回答
  • 2020-11-28 23:42

    RelativeLayout is a solution, if you don't want the button to stay too close to the bottom you can add margins (padding a button would break it because it uses a 9-patch background, and it sets its own padding).

    It would be the same if you used a LinearLayout: the way to achieve what you want is to set the ListView to a height=0 and weight=1, and for the button height=wrap_content and weight=0. (setting both to wrap_content, as user639183 said, would not limit ListView's height).

    0 讨论(0)
  • 2020-11-28 23:45

    On your last edit, you just have to also add android:layout_above="@+id/butt1" in your ListView code.

    And to show you(and everybody here who tried an answer earlier) that you really don't need to use more than one RelativeLayout, here is your corrected code:

       <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bkg">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="TextView1"
            android:layout_alignParentTop="true">
        </TextView>
        <TextView
            android:id="@+id/textView2"
            android:layout_below="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="TextView2"
            android:layout_centerHorizontal="true">
        </TextView>
        <Button
            android:id="@+id/butt1"
            android:text="string/string3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true">
        </Button>
        <ListView
            android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dip"
            android:layout_marginBottom="2dip"
            android:drawSelectorOnTop="false"
            android:visibility="visible"
            android:layout_below="@+id/textView2"
            android:layout_above="@+id/butt1" />
        </RelativeLayout>
    

    and the result, using some random list:
    enter image description here

    0 讨论(0)
  • 2020-11-28 23:48

    Using LinearLayout, set the height of your ListView and Button to wrap_content and add a weight=1 to ListView. This should work as you want.
    And yes, set the layout_gravity of the Button to bottom

    0 讨论(0)
  • 2020-11-28 23:51

    I solved this by putting the ListView inside a ScrollView. Lint didn't like it, but by adding a minHeight and setting the fillViewport to true, I have a nice result.

        <ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:minHeight="100dp"
            android:fillViewport="true"
            android:fadeScrollbars="false"
            android:paddingTop="2dp" 
            android:orientation="vertical"
            android:scrollbarAlwaysDrawVerticalTrack="true" >
    
            <ListView
                android:id="@+id/workoutAElistRoutines"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="fill_vertical"
                android:background="@drawable/dialog_inner_border_tan"
                android:choiceMode="singleChoice"
                android:orientation="vertical"
                android:paddingLeft="3dp"
                android:paddingRight="3dp" >
    
            </ListView>
        </ScrollView>        
    
    0 讨论(0)
提交回复
热议问题