Android,listView,我的文字不合适

时间:2013-12-13 01:32:27

标签: android listview imageview

我正在尝试使用2个TextView和2个ImageView创建一个ListView。 enter image description here

我希望“位置”TextView位于“名称”TextView下,而不是右侧!

以下是目前为止的XML代码:

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

    android:layout_width="fill_parent"
    android:height="?android:attr/listPreferredItemHeight"
    android:orientation="vertical">

    <View
        android:layout_width="fill_parent"
        android:layout_height="2px"
        android:background="?android:attr/listDivider" />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/list_item_widget_text"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/widget42"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Location"
            android:layout_below="@+id/list_item_widget_text"/>

        <ImageView

            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:src="@drawable/list_vertical_divider" />

        <ImageView
            android:id="@+id/list_item_widget_edit"
            android:background="@drawable/list_item_widget_bg"
            android:src="@android:drawable/ic_menu_edit"
            android:layout_height="fill_parent"
            android:layout_width="50dip"/>

        <ImageView

            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:src="@drawable/list_vertical_divider" />

        <ImageView
            android:id="@+id/list_item_widget_delete"
            android:background="@drawable/list_item_widget_bg"
            android:src="@android:drawable/ic_menu_delete"
            android:layout_height="fill_parent"
            android:layout_width="50dip" />


    </LinearLayout>
</LinearLayout>

提前谢谢!

2 个答案:

答案 0 :(得分:0)

将2 TextView包裹在LinearLayout这样的

  <LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical">     

        <TextView
            android:id="@+id/list_item_widget_text"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/widget42"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Location"
            android:layout_below="@+id/list_item_widget_text"/> 
<LinearLayout>

答案 1 :(得分:0)

虽然Apoorv的方法也很好,但我更倾向于使用RelativeLayout代替LinearLayout,这会给你更大的灵活性:

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

    <View
        android:layout_width="fill_parent"
        android:layout_height="2px"
        android:background="?android:attr/listDivider" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:id="@+id/list_item_widget_text"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_alignParentLeft="true"
            android:gravity="center_vertical" />

        <TextView
            android:id="@+id/widget42"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Location"
            android:layout_below="@+id/list_item_widget_text"/>

        <ImageView
            android:id="@+id/divider1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:layout_toLeftOf="@+id/list_item_widget_edit"
            android:src="@drawable/list_vertical_divider" />

        <ImageView
            android:id="@+id/list_item_widget_edit"
            android:background="@drawable/list_item_widget_bg"
            android:src="@android:drawable/ic_menu_edit"
            android:layout_height="fill_parent"
            android:layout_toLeftOf="@+id/divider2"
            android:layout_width="50dip"/>

        <ImageView
            android:id="@+id/divider2"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:layout_toLeftOf="@+id/list_item_widget_delete"
            android:src="@drawable/list_vertical_divider" />

        <ImageView
            android:id="@+id/list_item_widget_delete"
            android:background="@drawable/list_item_widget_bg"
            android:src="@android:drawable/ic_menu_delete"
            android:layout_alignParentRight="true"
            android:layout_height="fill_parent"
            android:layout_width="50dip" />


    </RelativeLayout>
</LinearLayout>