为什么too​​ls:listitem无法正常工作?

时间:2018-07-30 22:42:45

标签: android android-studio listview

我有这样一个列表项:

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

    <ImageView
        android:id="@+id/image_view_avatar"
        android:layout_width="@dimen/avatar_size"
        android:layout_height="@dimen/avatar_size"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="@dimen/margin"
        tools:src="@tools:sample/avatars"/>

    <TextView
        android:id="@+id/text_view_actor_name"
        android:layout_width="match_parent"
        android:layout_height="@dimen/avatar_size"
        android:layout_toEndOf="@+id/image_view_avatar"
        android:layout_toRightOf="@+id/image_view_avatar"
        android:layout_toStartOf="@+id/image_view_ocsar"
        android:layout_toLeftOf="@+id/image_view_ocsar"
        android:gravity="center_vertical"
        android:layout_margin="@dimen/margin"
        android:textSize="@dimen/text_size"
        android:maxLines="1"
        tools:text="@tools:sample/full_names"/>

    <ImageView
        android:id="@+id/image_view_ocsar"
        android:layout_width="@dimen/oscar_width"
        android:layout_height="@dimen/avatar_size"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="@dimen/margin"
        android:src="@drawable/oscar"/>

</RelativeLayout>

,预览效果如预期:

enter image description here

列表视图是:

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:listitem="@layout/list_item_actor">

</ListView>

但是为什么tools:listitem的listview预览不能正常工作?

enter image description here

为什么只有listview预览的第一项看起来像项目的预览,而其他项看起来却不同?..

2 个答案:

答案 0 :(得分:0)

删除标记点

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

<ImageView
    android:id="@+id/image_view_avatar"
    android:layout_width="@dimen/avatar_size"
    android:layout_height="@dimen/avatar_size"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_margin="@dimen/margin"
    tools:src="@tools:sample/avatars"/>

<TextView
    android:id="@+id/text_view_actor_name"
    android:layout_width="match_parent"
    android:layout_height="@dimen/avatar_size"
    android:layout_toEndOf="@+id/image_view_avatar"
    android:layout_toRightOf="@+id/image_view_avatar"
    android:layout_toStartOf="@+id/image_view_ocsar"  //remove this line
    android:layout_toLeftOf="@+id/image_view_ocsar"   //remove this line
    android:gravity="center_vertical"
    android:layout_margin="@dimen/margin"
    android:textSize="@dimen/text_size"
    android:maxLines="1"
    tools:text="@tools:sample/full_names"/>

<ImageView
    android:id="@+id/image_view_ocsar"
    android:layout_width="@dimen/oscar_width"
    android:layout_height="@dimen/avatar_size"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_margin="@dimen/margin"  //remove this line
    android:src="@drawable/oscar"/>
</RelativeLayout>

答案 1 :(得分:-1)

LinearLayout在您的情况下使用起来容易得多,因为我们可以使用layout_weight来消耗中心的剩余空间。

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

    <ImageView
        android:id="@+id/image_view_avatar"
        android:layout_width="@dimen/avatar_size"
        android:layout_height="@dimen/avatar_size"
        android:layout_margin="@dimen/margin"
        tools:src="@tools:sample/avatars" />

    <TextView
        android:id="@+id/text_view_actor_name"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/margin"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:maxLines="1"
        android:textSize="@dimen/text_size"
        tools:text="@tools:sample/full_names" />

    <ImageView
        android:id="@+id/image_view_ocsar"
        android:layout_width="@dimen/oscar_width"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/margin"
        android:src="@drawable/oscar"/>

</LinearLayout>