如何在ListView上方和下方放置小部件?

6

我有一个列表视图,想要在它的上方(y轴)和下方(y轴)放置包括图像、文本视图和一排按钮在内的内容。以下是我正在创建的简化版本。不幸的是,列表覆盖了头部,因此头部文本不可见,而不是在下面(y轴上)。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

    <TextView android:id="@+id/footer" 
        android:text="footer"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_alignParentBottom="true"
        />

    <ListView  android:id="@+id/list_view" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:layout_above="@id/footer"
        android:background="#ff9999ff"/>

    <TextView android:id="@+id/header" 
        android:text="header"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_alignParentTop="true"
        android:layout_above="@id/list_view"
        android:baselineAlignBottom="false"
        />

</RelativeLayout>

以下是相应的Activity类:

public class SampleListActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);

    }
}
4个回答

10

我无法在Android 1.5中让相对布局(Relative Layout)起作用,但是我成功让线性布局(Linear Layout)起作用了。以下是我的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:orientation="vertical">

    <TextView android:id="@+id/header" 
        android:text="header"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_weight="0"
        />
    <ListView  android:id="@+id/list_view" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:background="#ff9999ff"
        android:layout_weight="1"/>

    <TextView android:id="@+id/footer" 
        android:text="footer"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_weight="0"
        />
</LinearLayout>

5

使用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:id="@+id/header" 
        android:text="header"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" />

    <TextView android:id="@+id/footer" 
        android:text="footer"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_alignParentBottom="true"/>

    <ListView android:id="@id/android:list" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:layout_below="@id/header"
        android:background="#ff9999ff"/>

</RelativeLayout>

编辑: 根据建议删除了 android:orientation 属性


2
如果您的目标是Android 1.6及更高版本,请在现有的ListView上添加一个android:layout_below="@id/header"属性。
如果您仍然针对Android 1.5,则需要将其修改为android:layout_below="@+id/header",并且可能需要从TextView中的当前android:id="@+id/header"属性中删除+符号。
自1.6以来,RelativeLayout支持对其他小部件的前向引用,只要ID值的第一次出现带有+符号。

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接