首页 > 解决方案 > 什么是 Tools:itemCount 等效于 ExpandableListView?

问题描述

RecyclerView提供了一种在 Android Studio 的名为 Design 的选项卡中预览布局的便捷方式:

tools:itemCount

有什么等价物ExpandableListView吗?

我认为答案是“否”,因为我发现另一种工具tools:listitem确实适用于ExpandableListView. 我相信这可以通过它的文档来解释,“Intended for: <AdapterView>(and subclasses like <ListView>)”。

标签: androidandroid-studioandroid-layoutexpandablelistview

解决方案


是的你是对的。每个tools:属性属于不同的类。

您可以像下面这样集成这两个属性。

主要布局:

<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="match_parent"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    tools:context=".MainActivity">

    <ExpandableListView
        android:id="@+id/expandableListView"
        tools:listitem="@layout/sample_list_item" //notice this line
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="0.5dp" />

</RelativeLayout>

sample_list_item 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:itemCount="3"  //notice this line
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

现在您将在预览中看到如下图: 在此处输入图像描述

注意显示了三个项目。


推荐阅读