Android TabLayout 设置固定一屏,宽度不等怎么实现

效果图

xml

    <android.support.design.widget.TabLayout
        android:id="@+id/tl_tab"
        android:layout_width="wrap_content"
        android:layout_height="65px"
        android:layout_gravity="center_horizontal"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/color_FF0073cf"
        app:tabTextAppearance="@style/CreatePatientTabLayoutTextStyle"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/color_FF0073cf"
        app:tabTextColor="@color/color_999999" />

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

java

  public void setTabPadding() {
        Class tablayout = tlTab.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tablayout.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
            LinearLayout ll_tab = (LinearLayout) tabStrip.get(tlTab);
            for (int i = 0; i < ll_tab.getChildCount(); i++) {
                int width = 0;
                switch (i){
                    case 0:
                    case 3:
                    case 4:
                    case 5:
                        width = 100;
                        break;
                    case 1:
                    case 2:
                        width = 80;
                }
                View child = ll_tab.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
                child.setLayoutParams(params);
                child.invalidate(); // 这个方法是重画
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
阅读更多
个人分类: android
上一篇box-sizing: border-box ,允许width 100% ,padding在100%内部
下一篇mysql 表坏了
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭