TabLayout的选项卡之间的分隔线以编程方式LinearLayout.SHOW_DIVIDER_MIDDLE [英] Divider between Tabs of TabLayout Programatically LinearLayout.SHOW_DIVIDER_MIDDLE

查看:225
本文介绍了TabLayout的选项卡之间的分隔线以编程方式LinearLayout.SHOW_DIVIDER_MIDDLE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在制表符之间添加分隔符,而不使用任何自定义布局.我已经使用以下方法以编程方式对其进行了尝试:

I've tried adding the divider between tabs, without using any custom layout. I have tried it programmatically using the following method:

public static void addTabsDividers(TabLayout tabLayout, @ColorRes int divColorRes,int divWidthDP,int divHeightDP){
        View root = tabLayout.getChildAt(0);
        if (root instanceof LinearLayout) {
            ((LinearLayout) root).setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
            GradientDrawable drawable = new GradientDrawable();
            drawable.setColor(tabLayout.getContext().getResources().getColor(divColorRes));
            drawable.setSize(divWidthDP, divHeightDP);
//            ((LinearLayout) root).setDividerPadding(10);
            ((LinearLayout) root).setDividerDrawable(drawable);
        }
    }

,但这会在标签的左侧添加额外的空间:

but this adds extra space in left side of tabs:

请注意标签左侧的奇怪空白,我想删除它!

Notice the strange white space on left side of tabs, I want to remove that !!

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="?actionBarSize">
        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/tabs"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <FrameLayout
                android:id="@+id/fl_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabPaddingTop="5dp"
            app:tabPaddingBottom="0dp"
            app:tabPaddingStart="0dp"
            app:tabPaddingEnd="0dp"
            app:tabTextColor="@color/md_white_1000"
            app:tabSelectedTextColor="@color/md_white_1000"
            app:tabTextAppearance="@style/TabTextStyle"
            app:tabBackground="@drawable/tab_color_selector"/>
    </RelativeLayout>

推荐答案

我在您使用的同一问题上看到您,并且您使用的解决方案与我相同,并且我遇到了同样的问题!但是我发现为分隔线使用自定义可绘制对象100%解决了该问题,现在我在两个选项卡的中间只有一个分隔线.这是我正在使用的可绘制对象:

I saw you on the same question I was using and you are using the same solution as me and I have the same issue! But I found out using a custom drawable for the divider 100% solved the problem and now I only have a divider in the middle of my two tabs. This is the drawable I was using:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <size android:width="1dp" />
        <solid android:color="@android:color/white" />
    </shape>
</item>

<item
    android:bottom="16dp"
    android:top="12dp">

    <shape android:shape="rectangle">
        <solid android:color="@color/color_gray" />
        <size android:width="1dp" />
    </shape>
</item>

让我知道这是否对您有用.

let me know if this works for you.

public class MyTabLayout extends TabLayout {
public MyTabLayout(Context context) {
    super(context);
}

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

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

/**
 * Adds vertical dividers between tabs
 */
public void addTabDivider() {
    LinearLayout linearLayout = (LinearLayout)getChildAt(0);
    linearLayout
            .setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
    Drawable drawable = MyApp.getInstance().getDrawable(R.drawable.tab_divider);
    linearLayout.setDividerDrawable(drawable);
}

这篇关于TabLayout的选项卡之间的分隔线以编程方式LinearLayout.SHOW_DIVIDER_MIDDLE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆