RecyclerView项目的文本工具

7
我知道当您设置时,会发生以下情况:
tools:text="Sample text"

TextView中,您可以在Android Studio的预览模式下看到示例文本,但实际应用程序中却看不到。我希望能够在RecyclerView中的项目上实现此功能,但似乎无法做到。以下是我迄今为止所做的:

在名为content_feed的RecyclerView中:

tools:listitem="@layout/cell_feed"

在单元格中(名称为cell_feed):
tools:showIn="@layout/content_feed"

这里是xml文件:

cell_feed.xml


(Note: 没有需要翻译的解释)
<?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:layout_width="match_parent"
    android:layout_height="@dimen/height_feed_cell"
    android:layout_marginLeft="@dimen/margin_feed_cell"
    android:layout_marginRight="@dimen/margin_feed_cell"
    android:orientation="horizontal"
    tools:showIn="@layout/content_feed">

    <LinearLayout
        android:id="@+id/timeLayouts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="@dimen/alpha_feed_secondary_text"
            android:textSize="@dimen/size_feed_secondary_text"
            android:id="@+id/startTimeText"
            tools:text="8:00 AM"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="@dimen/alpha_feed_secondary_text"
            android:textSize="@dimen/size_feed_secondary_text"
            android:id="@+id/endTimeText"
            tools:text="10:00 AM"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_feed_cell_text"
        android:layout_toRightOf="@+id/timeLayouts"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin_bottom_feed_cell_title"
            android:textSize="@dimen/size_feed_cell_title"
            android:textStyle="bold"
            android:id="@+id/titleText"
            tools:text="Event title"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="@dimen/alpha_feed_secondary_text"
            android:textSize="@dimen/size_feed_secondary_text"
            android:id="@+id/captionText"
            tools:text="Event caption"/>
    </LinearLayout>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/margin_feed_cell_text"
        tools:text=""/>
</RelativeLayout>

content_feed.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/feedRecycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:listitem="@layout/cell_feed"
    tools:showIn="@layout/activity_feed"/>

1个回答

21
您想要的功能称为“支持示例数据”,最近在Google I/O 2017活动中宣布。这里是Tor Norbye介绍新功能的确切分钟链接。
例如,将以下内容应用于布局项:
tools:text="@tools:sample/lorem"

以下是在预览窗口中的输出结果:

Layout preview with lorem ipsum sample text

应用此:
tools:text="@tools:sample/date_day_of_week"

将在预览窗口中产生以下输出:

Layout preview with tools sample text

你也可以使用自定义数据来填充它。这篇博客文章演示了如何完整地进行操作。首先,右键单击您的应用程序模块,然后选择New | Sample Data directory
然后,在sampledata目录中创建一个activity_log.json文件,并填写以下内容:
{
    "activities" : [
     {
       "icon": "@sample/activity_icons[ic_biking.png]",
       "description": "Biking",
       "location" : "Pleasant Hill, CA",
       "distance": "48 miles",
       "date": "Yesterday"
     },
     // other items here
    ]
}

然后,您可以将这些数据应用于布局,如下所示:
tools:src="@sample/activity_log.json/activities/icon"
tools:src="@sample/activity_log.json/activities/distance"

这将在预览窗口中产生以下输出:

Layout preview with custom sample data

如果您想了解更多相关主题的详细信息,请查看Mark Allison的"工具时间"系列文章。


谢谢你提供的详细信息。我正在使用稳定通道(目前是Android Studio 2.3.2),所以似乎无法使用示例数据,但一旦3进入稳定状态,我就会使用它们。 - davidchuyaya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接