Android 布局,ListView 位于“顶栏”之间和“底栏”
我正在尝试构建一个布局,其中屏幕顶部有一个文本视图,屏幕底部有一个底部栏。 这些视图中的每一个都应该固定在适当的位置,并且在这两个视图之间应该是一个 ListView。
TOPBAR
LISTVIEW (scrollable)
BOTTOM BAR
我的布局(见下文)几乎有效:顶部和底部组件保持固定,列表视图滚动。 “问题”是我的 ListView 的最后一行仍然隐藏在底部栏后面。
关于如何调整布局有什么建议吗?
谢谢!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:background="@drawable/blackgrad"
android:gravity="center"
android:id="@+id/title"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:text="blah blah"
android:textColor="#ffffff"
android:textSize="18sp"
/>
<ListView
android:background="#ffffff"
android:cacheColorHint="#ffffffff"
android:id="@android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
<LinearLayout
android:background="#efefef"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:id="@+id/back"
android:text="Back"
android:layout_width="wrap_content" />
<Button
android:layout_height="wrap_content"
android:id="@+id/home"
android:text="Home"
android:layout_width="wrap_content" />
<Button
android:layout_height="wrap_content"
android:id="@+id/next"
android:text="Next"
android:layout_width="wrap_content" />
</LinearLayout>
</RelativeLayout>
I'm trying to construct a layout where there is a text view at the top of the screen and a bottom bar at the bottom of the screen.
Each of these views should stay fixed in place and in between the 2 should be a ListView.
TOPBAR
LISTVIEW (scrollable)
BOTTOM BAR
My layout (see below) almost works: the top and bottom components stay fixed and the listview scrolls. The "problem" is that the final row of my ListView remains hidden behind the bottom bar.
Any suggestions on how to adjust the layout?
Thanks!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:background="@drawable/blackgrad"
android:gravity="center"
android:id="@+id/title"
android:layout_height="50dip"
android:layout_width="fill_parent"
android:text="blah blah"
android:textColor="#ffffff"
android:textSize="18sp"
/>
<ListView
android:background="#ffffff"
android:cacheColorHint="#ffffffff"
android:id="@android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
<LinearLayout
android:background="#efefef"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:id="@+id/back"
android:text="Back"
android:layout_width="wrap_content" />
<Button
android:layout_height="wrap_content"
android:id="@+id/home"
android:text="Home"
android:layout_width="wrap_content" />
<Button
android:layout_height="wrap_content"
android:id="@+id/next"
android:text="Next"
android:layout_width="wrap_content" />
</LinearLayout>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里有一个简单的建议。创建一个新的 XML 并尝试一下。
Here comes a simple suggestion. Make a new XML and have a go with this.
OP 布局的问题在于,第一个线性布局设置为 android:layout_height="fill_parent" ,这将导致第一个线性布局填充父级。它不知道您要添加更多视图。您要做的就是按照视图知道其大小的顺序添加视图。所以您知道顶部栏和底部栏的大小,您可以将它们添加为
android:layout_height="wrap_content"
。然后添加列表视图,因为它的大小在编译时未知。The problem with OPs layout is that the first linear layout is set as
android:layout_height="fill_parent"
which will cause the first linear layout to fill the parent. It doesn't know that you're going to add further views. What you have to do is add views in the order that they know their size. So you know the size of the top par and the bottom bar, you can add those asandroid:layout_height="wrap_content"
. Then add the listview since it's size is unknown at compile time.我使用 ID 和layout_below/layout_above 属性以类似的方式完成了您想要做的事情。
View
的排列方式不同,但最终结果是相同的。一个位于另外两个LinearLayout
之间的ListView
,一个在顶部,一个在底部。在
ListView
中,您可以看到我有:这些属性将
ListView
定位在我的顶部和底部LinearLayout
之间。I accomplished what you're trying to do in a similar way, using IDs and the layout_below/layout_above attributes. The
View
s are arranged in a different way, but the end result is the same. AListView
between two otherLinearLayout
s, one on top and one on bottom.In the
ListView
you can see I have:Those attributes position the
ListView
between my top and bottomLinearLayout
s.我有这样的问题。我使用了三个布局而不是两个,这种解决方案:
的“bottombar”
<前><代码>
<文本视图
安卓:layout_width =“wrap_content”
安卓:layout_height =“match_parent”
机器人:layout_gravity =“center_vertical”
机器人:layout_marginTop =“10dip”
android:text="@string/show_history_text" />
<线性布局
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/secondLayout"
安卓:layout_width =“wrap_content”
安卓:layout_height =“wrap_content”
android:layout_below="@id/firstLayout"
android:layout_marginBottom="50dip" >
<列表视图
android:id="@+id/ListNotes"
安卓:layout_width =“fill_parent”
android:layout_height="wrap_content" />
<线性布局
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/thirdLayout"
安卓:layout_width =“fill_parent”
安卓:layout_height =“wrap_content”
机器人:layout_alignParentBottom =“真”
android:background="@color/LightSteelBlue"
机器人:重力=“左”
android:orientation="水平" >
<按钮
android:id="@+id/btnClose"
安卓:layout_width =“fill_parent”
安卓:layout_height =“wrap_content”
机器人:layout_gravity =“底部”
android:gravity="center_vertical|left"
android:onClick="BtnCloseClick"
android:text="@string/btn_close_history_text" //>
I had this kind of problem. I used three layouts instead of two, and this kind of solution :
and the most important thing (your problem) : I add android:layout_marginBottom to the layout containing the listview. I had to make several adjustement to the value before finding the sufficient margin.
当包含在线性布局中时,我找到了另一种方法来使用layout_weight来做到这一点。如果将 ListView 的权重设置为任意大的值,并将静态底部栏的权重设置为非常小的值,效果就会起作用。列表视图和底部栏的高度都必须设置为“wrap_content”才能使所有内容协同工作。
I've found another way to do this with layout_weight's when enclosed in a linear layout. If you set the ListView's weight to something arbitrarily large and the static bottom bar's weight to something very small, the effect works. Both the list view and the bottom bar's height's must be set to "wrap_content" to make everything work together as well.
我发现我认为更简单的解决方案来自 这篇文章
只需在视图列表之前声明两个栏,在每种情况下添加 android:layout_alignParentBottom="true" 和 android:layout_alignParentTop="true" 。
然后在视图列表布局中指定它在它们之间设置的 android:layout_above="id_of_footer" 和 android:layout_below="id_of_header" 属性。
这是我的应用程序的工作示例。只是有页脚栏。
I've found I think the easier solution just from this post
Just declare both bars BEFORE the viewlist adding android:layout_alignParentBottom="true" and android:layout_alignParentTop="true" in each case.
Then specify in the viewlist layout that it set between them with android:layout_above="id_of_footer" and android:layout_below="id_of_header" properties.
This is working example from my app. Just with footer bar.