工具:RecyclerView项目的文本

dav*_*aya 6 android android-studio android-recyclerview android-tools-namespace android-layout-editor

我知道你设置的时候

tools:text="Sample text"
Run Code Online (Sandbox Code Playgroud)

在a中TextView,您将在Android Studio的预览模式下看到示例文本,但不会在实际应用中看到.我想为a中的项目做这个RecyclerView,但我似乎无法做到.这是我到目前为止所做的:

在RecyclerView(名为content_feed)中:

tools:listitem="@layout/cell_feed"
Run Code Online (Sandbox Code Playgroud)

在单元格中(名称为cell_feed):

tools:showIn="@layout/content_feed"
Run Code Online (Sandbox Code Playgroud)

这是xml文件:

cell_feed.xml

<?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>
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)

azi*_*ian 19

您想要的功能称为"支持样本数据",最近在Google I/O 2017活动中宣布.是Tor Norbye引入新功能的确切分钟的直接链接.

例如,将以下内容应用于布局项:

tools:text="@tools:sample/lorem"
Run Code Online (Sandbox Code Playgroud)

将导致预览窗口中的以下输出:

使用lorem ipsum示例文本进行布局预览

应用于此:

tools:text="@tools:sample/date_day_of_week"
Run Code Online (Sandbox Code Playgroud)

将在预览窗口中导致此输出:

使用工具示例文本进行布局预览

您也可以使用自定义数据填充它.这篇博客文章演示了如何做到这一点的完整示例.首先,右键单击您的应用程序模块并选择New | Sample Data directory.

然后,您将activity_log.jsonsampledata目录中创建一个文件,其中包含以下内容:

{
    "activities" : [
     {
       "icon": "@sample/activity_icons[ic_biking.png]",
       "description": "Biking",
       "location" : "Pleasant Hill, CA",
       "distance": "48 miles",
       "date": "Yesterday"
     },
     // other items here
    ]
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过以下方式将此数据应用于布局:

tools:src="@sample/activity_log.json/activities/icon"
tools:src="@sample/activity_log.json/activities/distance"
Run Code Online (Sandbox Code Playgroud)

这将导致预览窗口中的以下输出:

使用自定义示例数据的布局预览

有关该主题的更多详细信息,请查看Mark Allison的"工具时间"系列文章.