带有ListView和button的Android布局

好的,这个特定的布局只是让我烦恼。 似乎无法find一种方法来获得一个listView,在底部有一排button,这样listview不会覆盖button的顶部,所以button总是被捕捉到屏幕的底部。 这是我想要的:

删除了死的ImageShack链接

这似乎应该是如此的容易,但我所尝试的一切都失败了。 任何帮助?

这是我现在的代码:

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); 

我想这就是你要找的。

 <?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> 

我有同样的问题多年。

将列表视图保持在button上方但防止在列表很长时覆盖它们的解决scheme是在ListView上设置android:layout_weight =“1.0”。 将layout_weight放置在未设置的button上,以保持其自然尺寸,否则button将缩放。 这适用于LinearLayout。

在Android ApiDemos中有一个例子: ApiDemos / res / layout / linear_layout_9.xml

我只是在寻找这个问题的答案,这是第一个结果。 我觉得好像所有的答案,包括目前被选为“最佳答案”的答案都不是解决被问到的问题。 正在陈述的问题是,有两个组件ButtonListView的重叠,因为ListView占用了整个屏幕,并且Button在ListView(阻止视图/访问ListView的最后一项)

根据我在这里和其他论坛看到的答案,我终于得出了如何解决这个问题的结论。

原来我有:

 <?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> 

注意使用RelativeLayout作为根节点。

这是最后的工作版本,其中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> 

只有两个区别。 首先,我已经转向使用LinearLayout 。 这将有助于下一步,这是添加android:layout_weight到我的ListView

我希望这有帮助。

最好的方法是在列表视图下面设置button的相对布局。 在这个例子中,button也是线性布局,因为它们更容易并排放置在相同的尺寸上。

 <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> 

我知道这篇文章是相当老,但回答原来的海报的问题,代码没有工作的原因是buttons.getId()返回-1。 如果你打算这样做,你需要做一些事情,如调用buttons.setId(10)。 如果你这样做,代码工作得很好。

这应该工作。 在列表视图上方也有button,把button放在另一个线性布局中。

 <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> 

最简单的解决scheme是创build两个线性布局,一个是button,另一个是列表视图(在button高度上包装内容,并在列表布局高度上匹配父级)。 那么只能用列表视图在布局上滚动视图,而button布局将被忽略。 希望它有帮助,对不起,我不想写出代码。