[Android] Viewpage2 + TabLayout 自定义Tab下, TabLayoutMediator关联后Tab消失问题

在Android应用中,当使用ViewPager2和自定义TabLayout时,通过TabLayoutMediator关联后发现自定义的Tab消失。问题在于,自定义Tab在MainActivity中添加的方式在结合TabLayoutMediator时被覆盖。正确的做法是在添加Tab时结合TabLayoutMediator进行操作,以避免自定义Tab消失。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

自定义的TabLayout在xml里面设置好,或者在代码里面自己添加好自定义的Tab后, 再结合 TabLayoutMediator关联ViewPage2, 发现Tablayout自定义的Tab都消失不见了…

代码展示

customer_tab_item.xml 自定义Tab 的xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/customer_tab_text"
        style="@style/customer_tab_item_text_style"
        android:layout_width="123dp"
        android:layout_height="0dp"
        android:layout_marginTop="12dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="@+id/lottieAnimationView"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintStart_toStartOf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值