我正在努力让页面布局正确,我试图提供尽可能简短和小的示例来说明我的问题。
我的目标是在屏幕顶部和底部有一个标题和页脚视图,中间有一个列表视图,另外还有一个视图(我们称之为“标签”,它是屏幕截图中的灰色框)。当需要滚动ListView
时,这个标签和页脚应该始终显示。
可视化结果
当ListView
不需要滚动时(这是正确的):
当ListView
需要滚动时,页脚和灰色框会被推出屏幕(错误):
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/red"/>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="@color/light_gray"
android:textColor="@color/black"/>
<!-- Used to push the footer to the bottom -->
<View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/blue"/>
</LinearLayout>
测试活动
public class TestActivity extends ListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<String> items = new ArrayList<String>();
items.add("one");
items.add("two");
items.add("three");
items.add("four");
items.add("five");
items.add("six");
items.add("seven");
items.add("eight");
items.add("nine");
items.add("ten");
setListAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1, items));
setContentView(com.myproject.android.R.layout.test);
}
}
我尝试了几种不同的方法,比如给
ListView
添加 layout_weight="1"
并删除用于将页脚推到底部的空白 View
。这基本上是我想要的,当 ListView
滚动时,它保持页脚和标签可见,但当仅有 1 或 2 个项目时,我需要在 ListView
正下方显示灰色框。我还尝试过使用 RelativeLayout
,但没有成功。我想我完全误解了一些东西。
编辑
这是我使用RelativeLayout
的尝试,但仍然不正确。<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/red"
android:id="@+id/header"
/>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_below="@id/header"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="@color/light_gray"
android:textColor="@color/black"
android:layout_below="@android:id/list"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/blue"
android:layout_alignParentBottom="true"
android:id="@+id/footer"/>
</RelativeLayout>
RelativeLayout
呢? - Renaud FavierListActivity
,我确实需要android:id/list
。另外,告诉页脚在灰色视图下方不会改变任何东西。如果您有一个可行的示例,请发布它。否则,这完全没有帮助。无论如何,还是谢谢。 - wsanville