Android中 TabLayout的TabItem文字和图片莫名不显示问题处理

Tablayout在没有设置适配器的时候,TabItem的文字和icon是正常显示的,可一旦设置上适配器文字和icon就直接消失,这个时候有两个解决办法(本人暂时只有两个)

1.效果图:

2. 正常写法设置适配器后的实图:

<android.support.design.widget.TabLayout
    android:id="@+id/fh_tab"
    app:tabIndicatorColor="@android:color/white"
    android:layout_marginTop="20dp"
    android:layout_below="@id/fh_title"
    android:layout_alignBottom="@+id/fh_iv"
    app:tabIndicatorHeight="4dp"
    app:tabTextColor="@color/tab_un_select"
    app:tabSelectedTextColor="@android:color/white"
    app:tabIndicatorFullWidth="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TabItem
        android:text="热点"
        android:icon="@drawable/ic_fire_gray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.design.widget.TabItem
        android:text="要闻"
        android:icon="@drawable/ic_news_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.design.widget.TabItem
        android:text="时政"
        android:icon="@drawable/ic_politics_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.design.widget.TabItem
        android:text="热点"
        android:icon="@drawable/ic_fire_gray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.design.widget.TabItem
        android:text="粤头条"
        android:icon="@drawable/ic_first_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.design.widget.TabItem
        android:text="分类"
        android:icon="@drawable/ic_classify_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.design.widget.TabLayout>

解决方法一:自己定TabItem的样式(比较万能,但是指示器的长度不太 “听话”,就是不受app:tabIndicatorFullWidth = "false"的控制,(自己去设置指示器的长度也没用,想试请看最后的"设置指示器长度代码")) :

1.创建一个item_home_tab:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
 
    <ImageView
        android:id="@+id/iv"
        android:src="@drawable/ic_relative_true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <TextView
        android:layout_marginTop="3dp"
        android:textColor="@color/font2"
        android:id="@+id/tv"
        android:textSize="13sp"
        android:text="title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
</LinearLayout>
 

2.java中:

protected void init(){
    fhPager.setAdapter(adapter);
    fhPager.setCurrentItem(0);
    fhPager.setOffscreenPageLimit(1);
    fhTab.setSelectedTabIndicator(R.drawable.bg_submit);
    fhTab.setupWithViewPager(fhPager);

    Objects.requireNonNull(fhTab.getTabAt(0)).setCustomView(getview("热点", R.drawable.ic_fire_state));
    Objects.requireNonNull(fhTab.getTabAt(1)).setCustomView(getview("要闻", R.drawable.ic_news_state));
    Objects.requireNonNull(fhTab.getTabAt(2)).setCustomView(getview("时政", R.drawable.ic_politics_state));
    Objects.requireNonNull(fhTab.getTabAt(3)).setCustomView(getview("粤头条", R.drawable.ic_fire_state));
    Objects.requireNonNull(fhTab.getTabAt(4)).setCustomView(getview("分类", R.drawable.ic_classify_state));
}

private View getview(String title, int icon) {
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_home_tab, null);
    TextView tv = view.findViewById(R.id.tv);
    ImageView iv = view.findViewById(R.id.iv);
    tv.setText(title);
    iv.setImageResource(icon);
    return view;
}

结果1:当Tablayout 的app:tabIndicatorFullWidth = "false"时出来的效果图:

结果2:当Tablayout 的app:tabIndicatorFullWidth = "true"出来的效果图:

 

解决办法二(指示器长度"听话"(受app:tabIndicatorFullWidth = "false"的控制),但是icon的大小就只能由系统来定):

fhPager.setAdapter(adapter);
fhPager.setCurrentItem(0);
fhPager.setOffscreenPageLimit(1);
fhTab.setSelectedTabIndicator(R.drawable.bg_submit);
fhTab.setupWithViewPager(fhPager);

//设置tabItem的文字和icon
fhTab.getTabAt(0).setText("热点").setIcon(R.drawable.ic_fire_state);
fhTab.getTabAt(1).setText("要闻").setIcon(R.drawable.ic_news_state);
fhTab.getTabAt(2).setText("时政").setIcon(R.drawable.ic_politics_state);
fhTab.getTabAt(3).setText("粤头条").setIcon(R.drawable.ic_first_state);
fhTab.getTabAt(4).setText("分类").setIcon(R.drawable.ic_classify_state);

结果1:当Tablayout 的app:tabIndicatorFullWidth = "false"时出来的效果图:

结果2:当Tablayout 的app:tabIndicatorFullWidth = "true"出来的效果图:

 

 

end...

设置TabLayout指示器的长度:

//指示器长度
public static void setIndicator(TabLayout tabs, int leftDip, int rightDip, int bottomDip) {
    if (tabs == null) {
        return;
    }
    Class<? extends TabLayout> tabLayout = tabs.getClass();

    Field tabStrip = null;
    try {
        tabStrip = tabLayout.getDeclaredField("mTabStrip");
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
        return;
    }
    tabStrip.setAccessible(true);
    LinearLayout llTab = null;
    try {
        llTab = (LinearLayout) tabStrip.get(tabs);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return;
    }
    int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
    int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
    int bottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, bottomDip, Resources.getSystem().getDisplayMetrics());
    for (int i = 0; i < llTab.getChildCount(); i++) {
        View child = llTab.getChildAt(i);
        child.setPadding(0, 0, 0, 0);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
        params.leftMargin = left;
        params.rightMargin = right;
        params.bottomMargin = bottom;
        child.setLayoutParams(params);
        child.invalidate();
    }
}
  • 22
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android TabLayout是一个用于显示标签页的控件,可以在布局使用TabLayout来创建并管理多个标签页。可以使用XML布局文件来定义TabLayout,并设置其属性来自定义样式和行为。 在XML布局,可以使用TabLayout标签来创建TabLayout控件,并使用TabItem标签来创建每个标签页的布局。可以设置TabItem的属性来定义每个标签页的文本、图标和样式。 如果标签页数量较多且超出屏幕宽度,可以使用属性tabMode="scrollable"来设置TabLayout可滚动,以便所有标签页都可以显示在屏幕上。 以下是一个示例的XML布局代码: ``` <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:tabTextColor="@color/colorPrimary" app:tabSelectedTextColor="@color/colorPrimaryDark" /> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/tab_layout" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 这段代码定义了一个包含TabLayout和ViewPager的ConstraintLayout布局。TabLayout用于显示标签页,ViewPager用于显示与标签页对应的内容页。你可以根据自己的需要修改这个布局,并在代码使用TabLayout和ViewPager来实现TabLayout的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android控件-TabLayout使用介绍](https://blog.csdn.net/csdnxia/article/details/105947804)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android控件——Tablayout使用浅析(一)](https://blog.csdn.net/weixin_43499030/article/details/90179867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值