TabLayout 固定个数

错误的效果

贴代码 

 注意把app:tabGravity="center" 去掉

MODE_FIXED是一下子展示所有的

mScrollableTabMinWidth = res.getDimensionPixelSize(R.dimen.design_tab_scrollable_min_width);是这句代码起的作用

<dimen name="design_tab_scrollable_min_width">72dp</dimen>设置大小

<com.test.test.tools.MyTabLayout
        android:id="@+id/order_tablayout"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="@color/transparent"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorff8b00"
        app:tabIndicatorHeight="2dp"
        app:tabMode="fixed"
        app:tabPaddingEnd="6dp"
        app:tabPaddingStart="6dp"
        app:tabSelectedTextColor="@color/colorff8b00"
        app:tabTextAppearance="@style/order_tab_text_size"
        app:tabTextColor="#afafaf" />
public class MyTabLayout extends TabLayout {
    private static final int TabViewNumber = 2;
    private static final String SCROLLABLE_TAB_MIN_WIDTH = "mScrollableTabMinWidth";

    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;

        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();
        }
    }
}

 

要将TabLayout固定在ScrollView的顶部,可以使用以下方法: 1. 使用CoordinatorLayout和AppBarLayout 将ScrollView作为CoordinatorLayout的子视图,并将AppBarLayout作为ScrollView的直接父视图。在AppBarLayout中添加TabLayout和Toolbar,然后使用app:layout_scrollFlags属性将Toolbar和TabLayout与ScrollView关联起来。 例如: ``` <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <com.google.android.material.appbar.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways"> <!-- Toolbar content --> </com.google.android.material.appbar.Toolbar> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" app:tabGravity="fill" app:tabMode="fixed"> <!-- TabLayout content --> </com.google.android.material.tabs.TabLayout> </com.google.android.material.appbar.AppBarLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- ScrollView content --> </ScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 2. 使用NestedScrollView和LinearLayout 将TabLayout和ScrollView放在一个LinearLayout中,然后将LinearLayout放在NestedScrollView中。在TabLayout中添加app:layout_scrollFlags属性,并将其设置为scroll|enterAlways。 例如: ``` <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:tabGravity="fill" app:tabMode="fixed"> <!-- TabLayout content --> </com.google.android.material.tabs.TabLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!-- ScrollView content --> </ScrollView> </LinearLayout> </androidx.core.widget.NestedScrollView> ``` 注意:在使用第二种方法时,TabLayout必须是LinearLayout的第一个子视图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值