android–Tabs不适合使用tabmode = scrollable的屏幕,即使使用自定义选项卡布局

android–Tabs不适合使用tabmode = scrollable的屏幕,即使使用自定义选项卡布局,第1张

概述我使用ViewPager创建了一个自定义TabLayout,并在可滚动模式下使用TabLayout:我需要它可以滚动,因为日期的数量可以变化到多达15-20:<com.example.project.recommendedapp.CustomScrollableTabLayoutandroid:id="@+idab_layout_movie"android:layout_width="match_pare

我使用VIEwPager创建了一个自定义TabLayout,并在可滚动模式下使用TabLayout:

我需要它可以滚动,因为日期的数量可以变化到多达15-20:

<com.example.project.recommendedapp.CustomScrollableTabLayout    androID:ID="@+ID/tab_layout_movIE"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="?attr/colorPrimary"    androID:elevation="6dp"    androID:minHeight="?attr/actionbarSize"    app:tabGravity="fill"    app:tabMode="scrollable"    app:tabTextAppearance="?androID:textAppearanceMedium"    androID:theme="@style/themeOverlay.AppCompat.Dark.Actionbar"/>

我按照另一个类似的问题使用的自定义类:
TabLayout not filling width when tabMode set to ‘scrollable’

自定义tablayout类是:

package com.example.project.recommendedapp;import androID.content.Context;import androID.graphics.Point;import androID.support.design.Widget.TabLayout;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.display;import androID.vIEw.WindowManager;import java.lang.reflect.FIEld;public class CustomScrollableTabLayout extends TabLayout {    private Context mContext;    Point size;    public CustomScrollableTabLayout(Context context) {        super(context);        mContext=context;        size = new Point();    }    public CustomScrollableTabLayout(Context context, AttributeSet attrs) {        super(context, attrs);        mContext=context;        size = new Point();    }    public CustomScrollableTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        mContext=context;        size = new Point();    }    @OverrIDe    protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) {        super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);        try {            if (getTabCount() == 0) {                return;            }else {                display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultdisplay();                display.getSize(size);                int wIDth = size.x;                FIEld fIEld = TabLayout.class.getDeclaredFIEld("mScrollableTabMinWIDth");                fIEld.setAccessible(true);                fIEld.set(this, (wIDth / getTabCount()));                Log.d("FragmentCreate",String.valueOf(wIDth / getTabCount()));            }        } catch (Exception e) {            Log.e("FragmentCreate","Error while setting wIDth",e);        }    }}

解决方法:

我全神贯过地寻找这个问题的答案.在我的情况下,我是动态添加和删除标签,所以我希望它只有几个标签填充屏幕,但是当有太多而不是缩小它们或将标题放在两行时开始滚动.使用以下自定义选项卡布局终于让它适合我.在调用super.onMeasure()之前设置最小宽度是关键.

public class CustomTabLayout extends TabLayout {    public CustomTabLayout(Context context) {        super(context);    }    public CustomTabLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @OverrIDe    protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) {        VIEwGroup tabLayout = (VIEwGroup)getChildAt(0);        int childCount = tabLayout.getChildCount();        if( childCount != 0 ) {             displayMetrics displayMetrics = getContext().getResources().getdisplayMetrics();            int tabMinWIDth = displayMetrics.wIDthPixels/childCount;            for(int i = 0; i < childCount; ++i){                tabLayout.getChildAt(i).setMinimumWIDth(tabMinWIDth);            }        }        super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);    }}

将选项卡模式设置为在xml中可滚动.

    <com.package.name.CustomTabLayout        androID:ID="@+ID/my_tabs"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        app:tabMode="scrollable">
总结

以上是内存溢出为你收集整理的android – Tabs不适合使用tabmode = scrollable的屏幕,即使使用自定义选项卡布局全部内容,希望文章能够帮你解决android – Tabs不适合使用tabmode = scrollable的屏幕,即使使用自定义选项卡布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1101698.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存