繁体   English   中英

如何在列表视图的上方和下方放置小部件?

[英]How do I place widgets above and below a listview?

我有一个列表视图,并希望将它们放在上面(在y轴上)和下面(y轴),包括图像,文本视图和一行按钮。 以下是我正在创建的简化版本。 不幸的是,列表覆盖了(即在z轴上方)标题,因此标题文本不可见而不是在下面(在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);

    }
}

我无法让相对布局在Android 1.5中运行,但我确实让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>

这应该使用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 1.6及更高版本,请在ListView上的现有属性中添加android:layout_below="@id/header"

如果您仍然以Android 1.5为目标,则需要制作android:layout_below="@+id/header"并可能从TextView当前的android:id="@+id/header"属性中删除+号。

RelativeLayout ,从1.6开始,支持对其他小部件的前向引用,只要第一次出现的ID值具有+号即可。

您只需要将重量1放在线性布局中的列表中,其他textviews / buttons / etc不需要具有任何重量值。

以下是一个示例: https//bitbucket.org/generalplus/android_development/src/5a892efb0551/samples/ApiDemos/res/layout/linear_layout_9.xml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM