如何使用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" />