Android上ListView的限制高度 [英] Limit height of ListView on Android
问题描述
我想在 ListView
下显示一个按钮.问题是,如果 ListView
被扩展(添加项目...),按钮会被推出屏幕.
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.
我尝试了带有权重的 LinearLayout
(如 Android:为什么没有视图的 maxHeight?),但要么是我的权重错误,要么就是根本不起作用.
I tried a LinearLayout
with weights (as suggested in Android: why is there no maxHeight for a View?), but either I got the weights wrong or it simply didn't work.
此外,我在某处找到了使用 RelativeLayout
的提示.然后将 ListView
设置在带有 android:layout_above
参数的按钮上方.
Also, I found somewhere the hint to use a RelativeLayout
. The ListView
would then be set above the button with the android:layout_above
param.
问题在于我不知道之后如何定位按钮.在我发现的示例中,ListView
下面的 View 是使用 android:layout_alignParentBottom
调整的,但我不希望我的按钮紧贴屏幕底部.
Problem with this is that I don't know how to position the button afterwards. In the example I found, the View below the ListView
was adjusted using android:layout_alignParentBottom
, but I don't want my button to cling to the bottom of the screen.
除了使用 setHeight 方法和计算所需空间之外还有什么想法吗?
Any ideas apart from using setHeight-method and some calculating of the required space?
我得到了很多有用的答案.
bigstone's &user639183 的解决方案几乎完美运行.但是,我不得不在按钮底部添加一个额外的内边距/边距,因为它仍然会被推出屏幕一半(但随后停止)
bigstone's & user639183's solution almost worked perfectly. However, I had to add an extra padding/margin to the bottom of the button, as it still would be pushed half way out of the screen (but then stopped)
如果您希望将按钮固定在屏幕底部,Adinia 对相对布局的回答就很好.这不是我想要的,但仍然可能对其他人有用.
Adinia's answer with the relative layout only is fine if you want the button fixed to the bottom of the screen. It's not what I intended but still might be useful for others.
AngeloS 的解决方案是我最后选择的解决方案,因为它刚刚创建了我想要的效果.但是,我对按钮周围的 LinearLayout
做了两个小改动:
AngeloS's solution was the one I chose at the end as it just created the effects I wanted. However, I made two minor changes to the LinearLayout
around the button:
首先,因为我不想在我的布局中有任何绝对值,所以我将
android:layout_height="45px"
更改为wrap_content
,即效果也很好.
First, as I didn't want to have any absolute values in my layout, I changed
android:layout_height="45px"
towrap_content
, which works just fine as well.
其次,由于我希望按钮水平居中,而这仅受垂直 LinearLayout
支持,因此我将 android:orientation="horizontal" 更改为vertical".
Second, as I wanted the button to be centered horizontally, which is only supported by vertical LinearLayout
, I changed android:orientation="horizontal" to "vertical".
AngeloS 在他最初的帖子中也表示,他不确定 ListView
LinearLayout 中的 android:layout_weight="0.1"
参数是否存在代码>有任何影响;我刚试过,确实如此!否则,按钮再次被推出屏幕.
AngeloS also stated in his initial post that he was not sure if the android:layout_weight="0.1"
param in the LinearLayout
around the ListView
had any effect; I just tried and it actually does! Without, the button gets pushed out of the screen again.
推荐答案
我遇到了这个确切的问题,为了解决它,我创建了两个单独的 LinearLayouts
来容纳 ListView
和Button
分别.从那里我将两者都放在另一个 Linear Layout
中,并将该容器的 android:orientation
设置为 vertical
.我还将 ListView
所在的 LinearLayout
的权重设置为 0.1
但我不知道这是否有任何影响.从那里,您可以将底部容器(带有您的按钮)的高度设置为您想要的任何高度.
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.
编辑这就是我的意思:
<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.
要使按钮浮动在列表底部,请将ListView01
的高度更改为wrap_content
:
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>
这篇关于Android上ListView的限制高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!