Java 带有 ListView 和按钮的 Android 布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2383847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Layout with ListView and Buttons
提问by Kleptine
Alright, this specific layout is just annoying me. And can't seem to find a way to have a listView, with a row of buttons at the bottom so that the listview doesn't extend over top of the buttons, and so the buttons are always snapped to the bottom of the screen. Here's what I want:
好吧,这个特定的布局让我很烦。并且似乎无法找到一种方法来拥有一个 listView,在底部有一排按钮,这样 listview 就不会延伸到按钮的顶部,所以按钮总是捕捉到屏幕的底部。这是我想要的:
removed dead ImageShack link
删除了无效的 ImageShack 链接
It seems like it should be so easy, but everything I've tried has failed. Any help?
看起来应该很容易,但我尝试过的一切都失败了。有什么帮助吗?
Here's my current code:
这是我当前的代码:
RelativeLayout container = new RelativeLayout(this);
container.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
//** Add LinearLayout with button(s)
LinearLayout buttons = new LinearLayout(this);
RelativeLayout.LayoutParams bottomNavParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
bottomNavParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bottomNavParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttons.setLayoutParams(bottomNavParams);
ImageButton newLayer = new ImageButton(this);
newLayer.setImageResource(R.drawable.newlayer);
newLayer.setLayoutParams(new LinearLayout.LayoutParams(45, LayoutParams.FILL_PARENT));
buttons.addView(newLayer);
container.addView(buttons);
//** Add ListView
layerview = new ListView(this);
RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
listParams.addRule(RelativeLayout.ABOVE, buttons.getId());
layerview.setLayoutParams(listParams);
container.addView(layerview);
采纳答案by lars
I think this is what you are looking for.
我想这就是你要找的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/testbutton"
android:text="@string/hello" android:layout_alignParentBottom="true" />
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/list"
android:layout_alignParentTop="true" android:layout_above="@id/testbutton" />
</RelativeLayout>
回答by Patrick Kafka
The best way is a relative layout that sets the buttons below the listview. In this example the buttons are also in a linear layout because it is easier to put them side by side at an equal size.
最好的方法是在列表视图下方设置按钮的相对布局。在这个例子中,按钮也是线性布局,因为更容易将它们并排放置在相同的大小。
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/ListView01"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_below="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button android:id="@+id/ButtonJoin"
android:text="Join"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true">
</Button>
<Button android:id="@+id/ButtonJoin"
android:layout_alignRight="@id/ButtonCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:layout_alignParentBottom="true">
</Button>
</LinearLayout>
</RelativeLayout>
回答by Doo Dah
I know this post is rather old, but, to answer the original poster's question, the reason the code did not work was buttons.getId() returns -1. If you are going to do this, you need to set do something like call buttons.setId(10). If you do that, the code works just fine.
我知道这篇文章很旧,但是,为了回答原始海报的问题,代码不起作用的原因是 button.getId() 返回 -1。如果你打算这样做,你需要设置做一些类似 call buttons.setId(10) 的事情。如果你这样做,代码工作得很好。
回答by David Ingram
I had the same problem for ages.
我有同样的问题多年。
The solution to keeping the ListView above the buttons, but preventing it from covering them up when the list is long, is to set android:layout_weight="1.0" on the ListView. Leave the layout_weight on the buttons unset so that they remain at their natural size, otherwise the buttons will get scaled. This works with LinearLayout.
将 ListView 保持在按钮上方但防止它在列表很长时覆盖它们的解决方案是在 ListView 上设置 android:layout_weight="1.0"。保持按钮上的 layout_weight 未设置,以便它们保持其自然大小,否则按钮将被缩放。这适用于 LinearLayout。
There's an example in the Android ApiDemos: ApiDemos/res/layout/linear_layout_9.xml
Android ApiDemos 中有一个例子: ApiDemos/res/layout/linear_layout_9.xml
回答by Dallas
I was just searching for an answer to this question and this was one of the first results. I feel as if all of the answers, including the one that is currently chosen as the "best answer" is not addressing the issue being asked about. The problem that is being stated is that there is an overlap of the two components Button
and ListView
in that the ListView is taking up the entire screen, and the Button is visually floating above (in front of) the ListView (blocking view/access of the last item in the ListView
)
我只是在寻找这个问题的答案,这是第一个结果。我觉得好像所有的答案,包括目前被选为“最佳答案”的答案,都没有解决被问到的问题。正在说明的问题是两个组件有重叠,Button
并且ListView
ListView 占据了整个屏幕,并且 Button 在视觉上浮动在 ListView 上方(前面)(阻止查看/访问最后一个中的项目ListView
)
Based on the answers I've seen here and on other forums, I finally came to a conclusion on how to resolve this.
根据我在这里和其他论坛上看到的答案,我终于得出了如何解决这个问题的结论。
Originally, I had:
最初,我有:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF394952">
<ListView
android:id="@+id/game_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
style="@android:style/ButtonBar">
<Button
android:id="@+id/new_game"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game"
android:textColor="#FFFFFFFF"
android:background="@drawable/button_background" />
</LinearLayout>
</RelativeLayout>
Note the use of RelativeLayout
as the root node.
注意RelativeLayout
作为根节点的使用。
This is the final, working version in which the Button does not overlap the ListView
:
这是最终的工作版本,其中 Button 不与 重叠ListView
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF394952">
<ListView
android:id="@+id/game_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="1.0" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
style="@android:style/ButtonBar">
<Button
android:id="@+id/new_game"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game"
android:textColor="#FFFFFFFF"
android:background="@drawable/button_background" />
</LinearLayout>
</LinearLayout>
There are only two differences. First, I've switched to using a LinearLayout
. This will help with the next bit, which was adding android:layout_weight
to my ListView
只有两个不同。首先,我已经切换到使用LinearLayout
. 这将有助于下一点,这是添加android:layout_weight
到我的ListView
I hope this helps.
我希望这有帮助。
回答by Rhys
this should work. to have buttons above the listview too, put the buttons inside another linear layout.
这应该有效。要在列表视图上方也有按钮,请将按钮放在另一个线性布局中。
<LinearLayout> main container // vertical
<LinearLayout> scrollview must be contained in a linear layout //vertical - height to fill parent
<ScrollView> set the height of this to fill parent
<ListView> will be contained in the scrollview
</ListView>
</ScrollView>
</LinearLayout>
<LinearLayout> //horizontal - height to wrap content
<Button>
</Button>
</LinearLayout>
</LinearLayout>
回答by Pete
the easiest solution would be to create two linear layouts, one with the button and the other with the list view(Wrap content on the button height and match parent on the list layout height). then only make a scroll view over the layout with the list view and the button layout will be ignored. hope it helps, sorry i didn't feel like writing out the code.
最简单的解决方案是创建两个线性布局,一个带有按钮,另一个带有列表视图(在按钮高度上包裹内容并在列表布局高度上匹配父级)。然后只使用列表视图在布局上进行滚动视图,按钮布局将被忽略。希望它有所帮助,对不起,我不想写出代码。