无法设置列表视图

时间:2014-12-05 05:46:22

标签: android xml android-layout

这是我的xml代码。我想将listview设置在按钮上方,在线性布局名称下面设置为“titlpricelinearlayout”。但我无法做到这一点。我放在按钮上方,但不在线性布局下方。这是我想要的照片

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

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/body_background"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="35dp"
            android:text="CURRENT ORDER"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFFFFF" />

        <LinearLayout
            android:id="@+id/titlpricelinearlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="20dp" >

            <TextView
                android:id="@+id/recipetitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Your recipes"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFFFFF" />

            <LinearLayout
                android:id="@+id/linearprice"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/txt_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:paddingRight="10dp"
                    android:text="-price"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#FFFFFF" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/recipeslist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttonorder"
        android:layout_alignParentLeft="true" >
    </ListView>

    <Button
        android:id="@+id/buttonorder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/footer"
        android:background="@drawable/red_button"
        android:text="ORDER"
        android:textColor="#FFFFFF" />

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

        <include layout="@layout/tabbar" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <include layout="@layout/header" />
    </LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

使用LinearLayout作为父级而不是Relativelayout并将给定的权重属性用于ListView,这样它将在屏幕上占据其余的高度:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <include layout="@layout/header" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/body_background"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="35dp"
            android:text="CURRENT ORDER"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFFFFF" />

        <LinearLayout
            android:id="@+id/titlpricelinearlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="20dp" >

            <TextView
                android:id="@+id/recipetitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Your recipes"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFFFFF" />

            <LinearLayout
                android:id="@+id/linearprice"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/txt_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:paddingRight="10dp"
                    android:text="-price"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#FFFFFF" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/recipeslist"
        android:layout_width="match_parent"
        android:layout_height="0d"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/buttonorder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/red_button"
        android:text="ORDER"
        android:textColor="#FFFFFF" />

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <include layout="@layout/tabbar" />
    </LinearLayout>

</LinearLayout>

答案 1 :(得分:0)

就是这样,我将liner2的高度更改为wrap_content并将android:layout_below="@id/layout2"放入Listview。

修改

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >
        <include layout="@layout/header" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/header"
        android:background="#FFFFFF"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="35dp"
            android:text="CURRENT ORDER"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFFFFF" />
        <LinearLayout
            android:id="@+id/titlpricelinearlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="20dp" >
            <TextView
                android:id="@+id/recipetitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Your recipes"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFFFFF" />
            <LinearLayout
                android:id="@+id/linearprice"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="horizontal" >
                <TextView
                    android:id="@+id/txt_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:paddingRight="10dp"
                    android:text="-price"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#FFFFFF" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <ListView
        android:id="@+id/recipeslist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttonorder"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/layout2" >
    </ListView>
    <Button
        android:id="@+id/buttonorder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/footer"
        android:background="#FF0000"
        android:text="ORDER"
        android:textColor="#FFFFFF" />
    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" >
        <include layout="@layout/tabbar" />
    </LinearLayout>
</RelativeLayout>

ListView