TabLayout tabMode="scrollable" 时,修改tab的宽度

使用TabLayout,但是 tabMode="scrollable" 时,两个tab之间的间距太开了,实在不美观,看TabLayout源码,原来是设置了最小间距导致的:

this.scrollableTabMinWidth = res.getDimensionPixelSize(dimen.design_tab_scrollable_min_width);
<dimen name="design_tab_scrollable_min_width">72dp</dimen>

ui工程师要求是47dp,于是通过反射的方法改变这个值就可以了:

public class MyTabLayout extends TabLayout {
    // 一屏显示多少个tab
    private static final int TabViewNumber = 8;
    // support 低版本可能不一样
    private static final String SCROLLABLE_TAB_MIN_WIDTH = "scrollableTabMinWidth";

    public MyTabLayout(Context context) {
        super(context);
        initTabMinWidth();
    }

    public MyTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTabMinWidth();
    }

    public MyTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTabMinWidth();
    }

    private void initTabMinWidth() {
//        int screenWidth = getResources().getDisplayMetrics().widthPixels;
//        int tabMinWidth = screenWidth / TabViewNumber;
        // scrollable模式时,tab的最小宽度
        int tabMinWidth = (int) getResources().getDimension(R.dimen.dp_47);

        Field field;
        try {
            field = TabLayout.class.getDeclaredField(SCROLLABLE_TAB_MIN_WIDTH);
            field.setAccessible(true);
            field.set(this, tabMinWidth);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

注意:

  • 这里是基于com.android.support:design:28.0.0,低版本字段名可能不一样
  • 注意添加混淆
## 不混淆 TabLayout 
-keepclasseswithmembernames class android.support.design.widget.TabLayout {
    *;
}

参考资料:https://blog.csdn.net/wanglaohushiwo/article/details/72857691

可以通过自定义TabLayout的TabView来实现设置每个Tab宽度。 1. 首先,创建一个自定义的TabView布局文件,例如tab_item.xml,设置TabView的宽度为固定值或者match_parent,例如: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@drawable/tab_text_color_selector" android:textSize="14sp"/> </LinearLayout> ``` 2. 在代码中设置TabLayout的自定义布局,例如: ```java TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab != null) { View tabView = LayoutInflater.from(this).inflate(R.layout.tab_item, null); TextView textView = tabView.findViewById(R.id.tab_text); textView.setText(tab.getText()); tab.setCustomView(tabView); } } // 设置每个Tab宽度 for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab != null && tab.getCustomView() != null) { View tabView = tab.getCustomView(); tabView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int width = tabView.getMeasuredWidth(); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tabView.getLayoutParams(); layoutParams.width = width; tabView.setLayoutParams(layoutParams); } } ``` 上面的代码中,首先设置了TabLayout的TabModeMODE_SCROLLABLE,表示Tab可以滚动。然后,为每个Tab设置自定义布局,并设置每个Tab宽度等于TabView的宽度。其中,通过measure方法测量TabView的宽度,然后将宽度设置给TabView的LayoutParams即可。 需要注意的是,如果TabLayout的TabModeMODE_FIXED,那么所有的Tab宽度都是相同的,无法设置不同的宽度
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值