Limit height of ListView on Android

后端 未结 16 1325
广开言路
广开言路 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:25

    This is the cleanest way to do it:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
      <ListView
          android:id="@+id/ListView01"
          android:layout_width="fill_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:dividerHeight="2px">
      </ListView>
      <FrameLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/drawable"
          >
        <Button android:background="@drawable/btn_more2"
            android:id="@+id/moreButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:paddingRight="20px"
            />    
      </LinearLayout>
    </LinearLayout>
    

    It is similar to Angelos's solution but you don't need the linearlayout that wraps up the ListView. Also you can use a FrameLayout which is lighter compare to a LinearLayout to wrap up the Button.

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

    Try to use RelativeLayout with button in the bottom. Set the height of the layout as "wrap_content". I haven't tried it. Just idea

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

    You can probably make this work using nested containers. It's possible that you might need to wrap the Button in a second (inner) container so that it takes up more space, and just align the Button to the top of the inner container.

    Possible something like this:

    RelativeLayout

    -- ListView

    -- RelativeLayout

    ---- Button

    Using the different alignment options on the two containers, you should be able to get this to work.

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

    Following is what worked for me...

     if(adapter.getCount() > 3){
                        View item = adapter.getView(0, null, impDateListView);
                        item.measure(0, 0);         
                        LayoutParams params = impDateListView.getLayoutParams();
                        params.width = LayoutParams.MATCH_PARENT;
                        params.height = 3 * item.getMeasuredHeight();
                        impDateListView.setLayoutParams(params);
    
    
                    }
    

    Note : The params.width =... Needs to be set also...

    The above example is when you use TableRow and ListView inside TableRow...

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

    I had this exact issue and to solve it I created two seperate LinearLayouts to house the ListView and Button respectively. From there I put both in another Linear Layout and set that container's android:orientation to vertical. I also set the weight of the the LinearLayout that housed the ListView to 0.1 but I dont know if that has any effect. From there, you can set the height of the bottom container (that has your button) to whatever height you would like.

    EDIT this is what i mean:

    <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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:orientation="horizontal">
    
        <ListView
            android:id="@+id/ListView01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:dividerHeight="2px"></ListView>
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="45px"
        android:background="@drawable/drawable"
        android:orientation="horizontal">
    
        <Button
            android:id="@+id/moreButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:background="@drawable/btn_more2"
            android:paddingRight="20px" />
    
    </LinearLayout>
    

    The above solution will fix the button the the bottom of the screen.


    To have the button float at the bottom of the list, change the height of ListView01 to wrap_content:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:orientation="horizontal">
    
        <ListView
            android:id="@+id/ListView01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:dividerHeight="2px"></ListView>
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="45px"
        android:background="@drawable/drawable"
        android:orientation="horizontal">
    
        <Button
            android:id="@+id/moreButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:background="@drawable/btn_more2"
            android:paddingRight="20px" />
    </LinearLayout>
    

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

    Since there's no MaxHeight attribute, your best bet is either to set the height of the ListView to a set value, or to use wrap_content.

    0 讨论(0)
提交回复
热议问题