如何使用android.support.design.widget.TabLayout创建标签的自定义布局? [英] How to use android.support.design.widget.TabLayout to create custom layout of tab?

查看:103
本文介绍了如何使用android.support.design.widget.TabLayout创建标签的自定义布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了以下有关如何设置 customView 的文档.但是,当我将布局中的 id 更改为"text1"时,和"icon", setText() setIcon()无效.

公共TabLayout.Tab setCustomView(int layoutResId)

设置用于此标签的自定义视图.

如果膨胀的布局包含ID为 text1 TextView ,则将使用 setText(CharSequence)的值进行更新.同样,如果此布局包含带有ID图标的 ImageView ,则它将使用为 setIcon(Drawable)提供的值进行更新.

(来源: http://developer.android.com/reference/android/support/design/widget/TabLayout.Tab.html#setCustomView(int))

任何人都可以给我举个例子吗?

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 of text1 then that will be updated with the value given to setText(CharSequence). Similarly, if this layout contains an ImageView with ID icon then it will be updated with the value given to setIcon(Drawable).

(Source: http://developer.android.com/reference/android/support/design/widget/TabLayout.Tab.html#setCustomView(int))

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆