如何使用 ListView 创建具有两个布局的 Android 布局

How can create a Android Layout with two layout with ListView

我无法正确设置 Android 布局!

在我的代码中,ListView 填满了屏幕。 如果我用数字更改 ListView 上的 layout_height,则对所有分辨率都不利!

我使用这个代码:

<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="@drawable/background">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/FirstLinearLayout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

        <ListView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/mainListView"
            >
        </ListView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/SecondLinearLayout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TableLayout
            android:layout_marginTop="10dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">
            <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:onClick="testButton"
            android:clickable="true"
            android:background="@drawable/search_button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:onClick="exitButton"
            android:layout_marginLeft="60dp"
            android:background="@drawable/exit_button" />
            </TableRow>
        </TableLayout>
    </LinearLayout>

</RelativeLayout>

但愿如此:

我哪里错了?

谢谢

您想这样做:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="1" >

<LinearLayout
    android:id="@+id/FirstLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="0.9"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/mainListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

<LinearLayout
    android:id="@+id/SecondLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:clickable="true"
        android:onClick="testButton" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:background="#000000"
        android:onClick="exitButton" />
</LinearLayout>

在第一个LinearLayout

中使用layout_above
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/FirstLinearLayout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/SecondLinearLayout"
        >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearlayout_second"
    android:layout_marginBottom="10dp"
    android:layout_weight="0.9"
    android:orientation="vertical">
    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

  <LinearLayout
    android:id="@+id/linearlayout_second"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_weight="0.1"
    android:gravity="center|bottom"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:onClick="testButton"
        android:text="button 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:text="button 2" />
</LinearLayout>
</RelativeLayout>