Android TabLayout 修改Indicator宽高 修改tab文字大小.选中加粗.

之前用的 tablayout+viewpager. 使用的系统默认的样式和系统提供的默认的公共方法.

现在已经不能满足新的需求了,需要进行一些修改.

Tablayout缺点:
1:不能指定指示器宽高
2:不能修改文字大小 且 选中和未选中文字大小是一致的.
 

修改方法: 拷贝design包下的Tablayout代码.新建一个类复制代码来修改.
 

//设置tab 未选中/选中 文字颜色
        tabLayout.setTabTextColors(getResources().getColor(R.color.text_color_black), getResources().getColor(R.color.text_color_black));
        //设置tab未选中/选中 文字大小
        tabLayout.setTabTextSize(DTLUtils.getPixels(40), DTLUtils.getPixels(43));
        //设置tab文字选中时加粗
        tabLayout.setTextSelectedBold(true);
        //设置指示器宽度
        tabLayout.setTabIndicatorWidth(DTLUtils.getPixels(40));
        //设置指示器高度
        tabLayout.setSelectedIndicatorHeight(DTLUtils.getPixels(6));


完整代码链接地址https://github.com/sufutian/Tablayout/blob/master/MyTabLayout.java

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,针对您的问题,我分别回答一下: 1. AndroidTab选中变大 您可以通过设置TabLayout选中Tab的自定义布局,来实现选中Tab变大的效果。具体步骤如下: 1)定义一个自定义的布局文件tab_item.xml,例如: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/tab_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="@color/tab_text_color" /> <View android:id="@+id/tab_indicator" android:layout_width="0dp" android:layout_height="2dp" android:layout_marginTop="6dp" android:background="@color/tab_indicator_color" /> </LinearLayout> ``` 2)在TabLayout中设置自定义的布局: ``` TabLayout.Tab tab1 = mTabLayout.newTab().setCustomView(R.layout.tab_item); TextView tabTitle1 = tab1.getCustomView().findViewById(R.id.tab_title); tabTitle1.setText("Tab1"); TabLayout.Tab tab2 = mTabLayout.newTab().setCustomView(R.layout.tab_item); TextView tabTitle2 = tab2.getCustomView().findViewById(R.id.tab_title); tabTitle2.setText("Tab2"); mTabLayout.addTab(tab1); mTabLayout.addTab(tab2); ``` 3)在TabLayout的OnTabSelectedListener监听器中,实现选中Tab变大的效果: ``` mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { View customView = tab.getCustomView(); if (customView != null) { TextView tabTitle = customView.findViewById(R.id.tab_title); View tabIndicator = customView.findViewById(R.id.tab_indicator); tabTitle.setTextSize(18); // 变大 tabIndicator.setVisibility(View.VISIBLE); } } @Override public void onTabUnselected(TabLayout.Tab tab) { View customView = tab.getCustomView(); if (customView != null) { TextView tabTitle = customView.findViewById(R.id.tab_title); View tabIndicator = customView.findViewById(R.id.tab_indicator); tabTitle.setTextSize(16); // 恢复原来的大小 tabIndicator.setVisibility(View.INVISIBLE); } } @Override public void onTabReselected(TabLayout.Tab tab) { } }); ``` 2. Android之解决TabLayout里面每个Tab项的间距和修改指示线的长度 您可以通过设置TabLayout的属性来实现修改Tab项的间距和指示线的长度。具体步骤如下: 1)修改Tab项的间距: ``` app:tabPaddingStart="10dp" app:tabPaddingEnd="10dp" ``` 2)修改指示线的长度: ``` app:tabIndicatorFullWidth="false" // 不占满Tab度 app:tabIndicatorWidth="30dp" // 指示线的度 ``` 3. Android修改选中字体大小 您可以在OnTabSelectedListener监听器中,实现选中Tab的字体大小变化。具体步骤如下: ``` mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { View customView = tab.getCustomView(); if (customView != null) { TextView tabTitle = customView.findViewById(R.id.tab_title); tabTitle.setTextSize(18); // 变大 } } @Override public void onTabUnselected(TabLayout.Tab tab) { View customView = tab.getCustomView(); if (customView != null) { TextView tabTitle = customView.findViewById(R.id.tab_title); tabTitle.setTextSize(16); // 恢复原来的大小 } } @Override public void onTabReselected(TabLayout.Tab tab) { } }); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值