问题
好吧,这个特定的布局只是让我烦恼。并且似乎无法找到一种方法来使用listView,底部有一排按钮,因此listview不会延伸到按钮顶部,因此按钮始终会捕捉到屏幕底部。这就是我想要的:
删除了死的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);
#1 热门回答(135 赞)
我想这就是你要找的东西。
<?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>
#2 热门回答(57 赞)
我多年来一直有同样的问题。
将ListView保持在按钮上方但阻止它在列表很长时覆盖它们的解决方案是在ListView上设置android:layout_weight ="1.0"。保留按钮上的layout_weight,使其保持自然大小,否则按钮将缩放。这适用于LinearLayout。
Android ApiDemos中有一个例子:ApiDemos/res/layout/linear_layout_9.xml
#3 热门回答(20 赞)
我只是在寻找这个问题的答案,这是最初的结果之一。我觉得好像所有答案,包括目前被选为"最佳答案"的答案都没有解决被问到的问题。正在陈述的问题是两个组件Button
和ListView
存在重叠,因为ListView占据整个屏幕,并且Button可视地浮动在ListView的上方(前面)(阻止视图/访问最后一个项目) theListView
)
基于我在这里和其他论坛上看到的答案,我终于得出了如何解决这个问题的结论。
最初,我有:
<?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>
只有两个不同之处。首先,我转而使用aLinearLayout
。这将有助于下一位,即添加android:layout_weight
到myListView
我希望这有帮助。