如何使用android.support.design.widget.TabLayout创建标签的自定义布局? [英] How to use android.support.design.widget.TabLayout to create custom layout of tab?
问题描述
我找到了以下有关如何设置 customView
的文档.但是,当我将布局中的 id
更改为"text1"时,和"icon", setText()
和 setIcon()
无效.
公共TabLayout.Tab setCustomView(int layoutResId)
设置用于此标签的自定义视图.
如果膨胀的布局包含ID为
text1
的TextView
,则将使用setText(CharSequence)
的值进行更新.同样,如果此布局包含带有ID图标的ImageView
,则它将使用为setIcon(Drawable)
提供的值进行更新.
任何人都可以给我举个例子吗?
Java代码:
TabLayout.Tab tabAdd = tabLayout.getTabAt(0);tabAdd.setCustomView(R.layout.tab_layout_custom_view);tabAdd.setText(添加");tabAdd.setIcon(R.mipmap.add_tab).setText("Add");
布局代码:
< ImageViewandroid:layout_width =" match_parent"android:layout_height =" wrap_content"android:layout_weight =" 1"android:id ="@@ id/icon"/>< TextViewandroid: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
.
< ImageViewandroid:layout_width ="match_parent"android:layout_height ="wrap_content"android:layout_weight ="1"android:id ="@ android:id/icon"/>< TextViewandroid: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
.>
I found the below documentation on how to set a customView
. But when I change the id
in my layout to "text1" and "icon", setText()
and setIcon()
do not work.
public TabLayout.Tab setCustomView (int layoutResId)
Set a custom view to be used for this tab.
If the inflated layout contains a
TextView
with an ID oftext1
then that will be updated with the value given tosetText(CharSequence)
. Similarly, if this layout contains anImageView
with ID icon then it will be updated with the value given tosetIcon(Drawable)
.
Can anyone give me an example of how this works?
Java code:
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");
Layout Code:
<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" />
You need to use the system Resource identifiers. That is, @android:id/text1
and @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" />
If you would need to reference these IDs in your code, they would be android.R.id.text1
and android.R.id.icon
.
这篇关于如何使用android.support.design.widget.TabLayout创建标签的自定义布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!