【Android】tabLayout改变标签文字大小时发生的跳闪问题解决方法

在这里插入图片描述

如上图,tabLayout在选中时改变文字大小。

贴下出问题的代码

test.xml布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:layout_gravity="center"
        android:text="123"
        android:textColor="@color/white"
        android:textSize="30px" />

</FrameLayout>

        TabLayoutMediator mediator = new TabLayoutMediator(tabLayout, viewPager2, true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.test, null, false);
                tab.setCustomView(view);
            }
        });

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ViewGroup view = (ViewGroup) tab.getCustomView();
                TextView tv1  = (TextView) view.getChildAt(0);
                tv1.setTextSize(50f);//为了方便测试,随便写个大小
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ViewGroup view = (ViewGroup) tab.getCustomView();
                TextView tv1  = (TextView) view.getChildAt(0);
                tv1.setTextSize(30f);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        mediator.attach();

上面这种写法,会导致左右滑动indicator切换发生跳闪,原因是TextView.setTextSize会导致requestLayout(),造成动画重新开始

下面给出其中一种解决方法,套两个textview
上代码test.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:layout_gravity="center"
        android:text="123"
        android:textColor="@color/white"
        android:textSize="24sp"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:layout_gravity="center"
        android:text="456"
        android:textColor="@color/white"
        android:textSize="20sp" />

</FrameLayout>
//其他部分一样,就不重新贴了
 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ViewGroup view = (ViewGroup) tab.getCustomView();
                TextView tv1  = (TextView) view.getChildAt(0);
                TextView tv2  = (TextView) view.getChildAt(1);

                tv1.setVisibility(View.VISIBLE);
                tv2.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                int position = tab.getPosition();

                ViewGroup view = (ViewGroup) tab.getCustomView();
                TextView tv1  = (TextView) view.getChildAt(0);
                TextView tv2  = (TextView) view.getChildAt(1);

                tv1.setVisibility(View.INVISIBLE);
                tv2.setVisibility(View.VISIBLE);

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值