TabLayout指示宽度设置

在这里插入图片描述
在使用系统控件时有时候默认效果往往不是需要的,有这方面需要的可以参考下。

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tl_title"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="@dimen/size_20dp"
    android:layout_marginRight="@dimen/size_20dp"
    app:tabIndicatorColor="#da9c26"
    app:tabIndicatorFullWidth="false"
    app:tabIndicatorHeight="5dp"
    app:tabRippleColor="@android:color/transparent"
    app:tabSelectedTextColor="#da9c26"
    app:tabIndicator="@drawable/tab_indicator_da9c26"
    app:tabTextColor="#888888">
</com.google.android.material.tabs.TabLayout>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<item android:gravity="center">
    <shape>
        <size
            android:width="26dp"
            android:height="4dp" />
        <corners android:radius="10dp" />
        <solid android:color="#da9c26" />
    </shape>
</item>
</layer-list>
//必须先写监听后添加标签设置,否则首次不执行
tl_title.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        replaceFragments(fragments.get(tab.getPosition()));
    }


    @Override
    public void onTabUnselected(TabLayout.Tab tab) {


    }


    @Override
    public void onTabReselected(TabLayout.Tab tab) {


    }
});
tl_title.addTab(tl_title.newTab().setText("成长历程"), true);
tl_title.addTab(tl_title.newTab().setText("赛事专属"));
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Android TabLayout 的指示器颜色设置为渐变色,可以执行以下步骤: 1. 定义渐变色 在 drawable 目录下创建一个 gradient.xml 文件,定义渐变色。例如: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="@color/colorStart" android:endColor="@color/colorEnd" android:angle="0" /> </shape> ``` 其中,@color/colorStart 和 @color/colorEnd 分别指定渐变色的起始和结束颜色,android:angle 表示渐变的角度。 2. 设置 TabLayout 的指示器颜色 通过 TabLayout.setSelectedTabIndicatorColor() 方法设置 TabLayout 的指示器颜色。这里将渐变色作为指示器的颜色: ``` TabLayout tabLayout = findViewById(R.id.tabLayout); tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.transparent)); GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setColor(ContextCompat.getColor(this, R.color.transparent)); gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT); gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT); gradientDrawable.setColors(new int[]{ ContextCompat.getColor(this, R.color.colorStart), ContextCompat.getColor(this, R.color.colorEnd) }); tabLayout.setSelectedTabIndicator(gradientDrawable); ``` 其中,tabLayout.setSelectedTabIndicatorColor() 方法设置指示器的颜色为透明,防止出现默认的颜色叠加在渐变色上的情况。然后创建一个 GradientDrawable 对象,设置渐变色的起始和结束颜色,以及渐变的方向。最后调用 tabLayout.setSelectedTabIndicator() 方法将渐变色作为指示器的背景。 3. 设置 TabLayout 的指示器高度 通过 TabLayout.setSelectedTabIndicatorHeight() 方法设置 TabLayout 的指示器高度。例如: ``` tabLayout.setSelectedTabIndicatorHeight(getResources().getDimensionPixelSize(R.dimen.indicator_height)); ``` 其中,R.dimen.indicator_height 是在 dimens.xml 文件中定义的指示器高度的值。 这样,就可以将 Android TabLayout 的指示器颜色设置为渐变色,并且可以设置指示器的高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值