Android:列表视图的布局问题

时间:2011-06-09 14:33:08

标签: android xml layout android-linearlayout

我正在尝试为我的listview元素添加标题。现在所有信息都是从一个静态数组中填充的,该数组由几个元素组成,但现在基本上是一个Image,后跟几个不同的textview。我想在图像和文本视图上方有一个标题栏,但仍然包含在scrollview元素中(这有意义吗?我还在尝试编写术语,哈哈。)我试过添加一个相对布局/ imageview上方的textview / tablerow无济于事。

这是LayoutCode

<?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="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >
    <ImageView
        android:id="@+id/icon"
        android:layout_width="100sp"
        android:layout_height="100sp"
        android:layout_marginRight="6dip"
        android:src="@drawable/car1" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent"
        >
        <TextView
            android:id="@+id/make"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textColor="#000"
        />

    </LinearLayout>
</LinearLayout>

这包含在:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RelativeLayout android:orientation="vertical" android:background="@drawable/banner"
        android:layout_width="fill_parent" android:layout_height="50sp" android:gravity="center" >

    </RelativeLayout>
    <RelativeLayout android:orientation="vertical"
        android:background="#FFFFF0" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:gravity="fill_vertical">
        <Button android:text="Change Location" android:id="@+id/ListingsLocationbtn"
            android:layout_height="wrap_content" android:gravity="center"
            android:layout_width="100sp" android:layout_margin="3sp"></Button>
        <Button android:id="@+id/ListingsDetailsbtn" android:text="Details"
            android:layout_toRightOf="@+id/ListingsLocationbtn"
            android:layout_alignTop="@+id/ListingsLocationbtn"
            android:layout_alignBottom="@+id/ListingsLocationbtn"
            android:layout_height="match_parent" android:gravity="center|center_vertical"
            android:layout_width="100sp" android:layout_margin="3sp"></Button>
        <Button android:layout_height="wrap_content" android:id="@+id/ListingsFilterbtn"
            android:text="Filter Results" android:layout_alignParentRight="true"
            android:layout_width="100sp" android:gravity="center"
            android:layout_margin="3sp"></Button>
    </RelativeLayout>

    <RelativeLayout android:orientation="vertical"
        android:background="#FFFFF0" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:gravity="fill_vertical">
        <TextView android:id="@+id/zipcodeTV" android:paddingTop="2sp"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:textColor="#000"  android:textStyle="bold"/>
    </RelativeLayout>

    <ListView android:id="@+id/android:list" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:background="#FFFFF0" />
    <TextView android:id="@+id/android:empty"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:text="@string/main_no_items" android:textColor="#000" />

</LinearLayout>

*我删除了一些textview,因为我的代码没有占用整个页面

enter image description here

提前感谢您的帮助:)!

1 个答案:

答案 0 :(得分:4)

尝试这样的事情

<?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="wrap_content"
        android:orientation = "vertical"
        android:padding="6dip" >
        <TextView
            android:id = "@+id/myTextBar"
            android:layout_width = "fill_parent"
            android:layout_height = "wrap_content"`
            android:text = "This is my text bar over my image and other info"/>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation = "horizontal"
            android:padding="6dip" >
            <ImageView
                android:id="@+id/icon"
                android:layout_width="100sp"
                android:layout_height="100sp"
                android:layout_marginRight="6dip"
                android:src="@drawable/car1" />
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dip"
                android:layout_weight="1"
                android:layout_height="fill_parent">
                <TextView
                    android:id="@+id/make"
                    android:layout_width="fill_parent"
                    android:layout_height="0dip"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:textColor="#000"/>

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

enter image description here

如果你想用相对布局来做,这就是它的样子:

<?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 = "wrap_content"
    android:padding="6dip">
    <TextView
        android:id = "@+id/title_bar_text"
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:layout_alignParentTop = "true"
        android:text = "This is the title bar"
        android:gravity = "center_horizontal"/>
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical = "true"
        android:layout_alignParentLeft="true"
        android:layout_below = "@id/title_bar_text"
        android:layout_marginRight="6dip"
        android:src="@drawable/icon" />

    <TextView
        android:id="@+id/make"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon"
        android:layout_below = "@id/title_bar_text"
        android:layout_alignParentRight="true"
        android:layout_alignBottom = "@id/icon"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:textColor="#FFFFFFFF"
        android:textStyle="bold"
        android:layout_alignWithParentIfMissing="true"
        android:text = "This is the text beside the image"/>
 </RelativeLayout>

看起来像这样:

enter image description here