Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用android.support.design.widget.TabLayout创建选项卡的自定义布局?_Android - Fatal编程技术网

如何使用android.support.design.widget.TabLayout创建选项卡的自定义布局?

如何使用android.support.design.widget.TabLayout创建选项卡的自定义布局?,android,Android,我找到了下面关于如何设置自定义视图的文档。但是当我将布局中的id更改为“text1”和“icon”时,setText()和setIcon()不起作用 公共TabLayout.Tab setCustomView(int layoutResId) 设置用于此选项卡的自定义视图 如果充气布局包含ID为text1的TextView,则该视图将使用setText(CharSequence)的值进行更新。类似地,如果此布局包含一个带有ID图标的ImageView,则将使用setIcon(可绘制)的值进行更新

我找到了下面关于如何设置
自定义视图的文档。但是当我将布局中的
id
更改为“text1”和“icon”时,
setText()
setIcon()
不起作用

公共TabLayout.Tab setCustomView(int layoutResId)

设置用于此选项卡的自定义视图

如果充气布局包含ID为
text1
TextView
,则该视图将使用
setText(CharSequence)
的值进行更新。类似地,如果此布局包含一个带有ID图标的
ImageView
,则将使用
setIcon(可绘制)
的值进行更新

(来源:)

有人能给我举个例子说明这是怎么回事吗

Java代码:

    TabLayout.Tab tabAdd = tabLayout.getTabAt(0);
    tabAdd.setCustomView(R.layout.tab_layout_custom_view);
    tabAdd.setText("Add");
    tabAdd.setIcon(R.mipmap.add_tab).setText("Add");
布局代码:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@+id/icon"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:id="@+id/text1"
    android:gravity="center"
    android:layout_below="@+id/icon" />

您需要使用系统资源标识符。也就是说,
@android:id/text1
@android:id/icon

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@android:id/icon"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:id="@android:id/text1"
    android:gravity="center"
    android:layout_below="@android:id/icon" />


如果您需要在代码中引用这些id,它们将是值得一读的
android.R.id.text1
android.R.id.icon

。您也可以膨胀一个您自己管理的自定义视图,在这里查看完整的实现:我为感兴趣的任何人找到了关于这个的更多信息。
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:id="@android:id/icon"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:id="@android:id/text1"
    android:gravity="center"
    android:layout_below="@android:id/icon" />