在Android上限制ListView的高度

jel*_*ish 88 layout android listview android-layout

我想在一个下面显示一个按钮ListView.问题是,如果ListView扩展(添加了项目......),按钮将被推出屏幕.

我尝试了一个LinearLayout带权重(如Android所示:为什么没有一个视图的maxHeight?),但要么我的权重错了,要么根本不起作用.

另外,我发现某处使用了一个提示RelativeLayout.在ListView随后将被与按钮上方设置android:layout_abovePARAM.

问题是我不知道如何在按钮后放置按钮.在我发现的示例中,使用下面的视图ListView进行了调整android:layout_alignParentBottom,但我不希望我的按钮粘在屏幕的底部.

除了使用setHeight方法和一些计算所需空间之外的任何想法?


编辑: 我得到了很多有用的答案.

  • bigstone的用户和用户639183的解决方案几乎完美无缺.但是,我不得不在按钮的底部添加额外的填充/边距,因为它仍然会被推出屏幕的一半(但随后停止)

  • 如果您希望按钮固定在屏幕底部,Adinia对相对布局的回答很好.这不是我的意图,但仍可能对其他人有用.

  • AngeloS的解决方案是我最后选择的解决方案,因为它创造了我想要的效果.但是,我LinearLayout对按钮周围做了两处小改动:

    • 首先,因为我不希望在我的布局任何绝对值,我换android:layout_height="45px"wrap_content,其工作也很不错.

    • 其次,因为我希望按钮水平居中,只有垂直支持LinearLayout,我将android:orientation ="horizo​​ntal"改为"vertical".

    AngeloS在他的帖子中也说过,他不确定周围的android:layout_weight="0.1"参数是否有任何影响; 我刚试过,它确实做到了!没有,按钮再次被推出屏幕.LinearLayoutListView

sha*_*ylh 62

我在代码中解决了这个问题,只有当listview中有5个以上的项目时才设置它的高度:

if(adapter.getCount() > 5){
        View item = adapter.getView(0, null, listView);
        item.measure(0, 0);         
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, (int) (5.5 * item.getMeasuredHeight()));
        listView.setLayoutParams(params);
}
Run Code Online (Sandbox Code Playgroud)

请注意,我将最大高度设置为单个项目高度的5.5倍,因此用户肯定知道有一些滚动要做!:)

  • 这是最好的答案.当你用5行代码解决问题时,为什么要编写50行嵌套XML? (2认同)

Ang*_*loS 52

我有这个确切的问题,为了解决这个问题,我创建了两个单独的分别LinearLayouts来容纳ListViewButton.从那里我把两个放在另一个Linear Layout容器android:orientationvertical.我还设置了重量LinearLayout,里面圈着ListView0.1,但我不知道是否有任何影响.从那里,您可以将底部容器(具有您的按钮)的高度设置为您想要的任何高度.

编辑这就是我的意思:

<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:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        >   



                  <ListView android:id="@+id/ListView01" 
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"
                    android:dividerHeight="2px">
                 </ListView>



    </LinearLayout> 

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="45px"
    android:background="@drawable/drawable" 
    > 


                <Button android:background="@drawable/btn_more2"                     
                     android:id="@+id/moreButton"
                     android:layout_width="wrap_content"
                     android:layout_height="fill_parent"
                     android:layout_alignParentRight="true"
                     android:paddingRight="20px"
                     />

</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

上面的解决方案将按钮固定在屏幕的底部.


要使按钮浮动在列表的底部,请将高度更改ListView01wrap_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:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        >   

        <ListView android:id="@+id/ListView01" 
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"
                    android:dividerHeight="2px">
                 </ListView>



    </LinearLayout> 

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="45px"
    android:background="@drawable/drawable" 
    > 


                <Button android:background="@drawable/btn_more2"                     
                     android:id="@+id/moreButton"
                     android:layout_width="wrap_content"
                     android:layout_height="fill_parent"
                     android:layout_alignParentRight="true"
                     android:paddingRight="20px"
                     />

</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 没有冒犯,但这听起来像很多开销!:) (4认同)
  • 太棒了,真的有用!:-) 真的非常感谢你! (2认同)
  • 不需要包含所有这些视图 - 查看[@ Sparkle的解决方案](http://stackoverflow.com/a/17254622/383414) (2认同)

Adi*_*nia 21

在上次编辑时,您还必须添加android:layout_above="@+id/butt1"ListView代码.

并告诉你(以及之前尝试过答案的每个人)你真的不需要使用多个RelativeLayout,这是你更正的代码:

   <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>
Run Code Online (Sandbox Code Playgroud)

结果,利用一些随机列表:
在此输入图像描述


ern*_*azm 6

使用LinearLayout,设置你的高度ListView,并Buttonwrap_content和添加权重= 1 ListView.这应该按你的意愿工作.
是的,设定layout_gravityButtonbottom


big*_*nes 5

RelativeLayout 是一个解决方案,如果你不希望按钮太靠近底部你可以添加边距(填充按钮会打破它,因为它使用9补丁背景,并设置自己的填充).

如果您使用以下内容将是相同的LinearLayout:实现您想要的方法是设置ListView高度= 0和权重= 1,并且按钮高度= wrap_content和weight = 0.(设置为wrap_content,如user639183所说,不会限制ListView高度).


小智 5

在AngeloS的解决方案的启发下,找到了一种不使用嵌套容器的方法.

我使用了LinearLayout一个垂直方向,有一个ListView和一个按钮.我android:layout_weight="0.1"为此设定了ListView.

它设法让按钮始终位于列表的底部.当列表增长时,按钮不会被推离屏幕.

<ListView
    android:id="@+id/notes_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"        
    />

<Button
    android:id="@+id/create_note_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:onClick="onCreateButtonClicked"
    android:text="@string/create_notes"
    />
Run Code Online (Sandbox Code Playgroud)

  • 不,只是尝试了它,按钮停留在屏幕的底部,它不会停留在列表的底部.:( (2认同)
  • @Richard Le Mesurier事实上,对于LinearLayout的`android:layout_height ="wrap_content"`,就像在你的解决方案中一样,它工作得很好,不仅仅是4.4.3,但是由于Sparkle没有提到它,我试过了之前使用`match_parent`. (2认同)