Android 表格布局选项卡之间的分隔符以编程方式线性布局。显示分隔符
我尝试在选项卡之间添加分隔符,而不使用任何自定义布局。我已使用以下方法以编程方式进行了尝试:Android 表格布局选项卡之间的分隔符以编程方式线性布局。显示分隔符,android,android-tablayout,divider,Android,Android Tablayout,Divider,我尝试在选项卡之间添加分隔符,而不使用任何自定义布局。我已使用以下方法以编程方式进行了尝试: public static void addTabsDividers(TabLayout tabLayout, @ColorRes int divColorRes,int divWidthDP,int divHeightDP){ View root = tabLayout.getChildAt(0); if (root instanceof LinearLayout) {
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);
}
}
但这会在选项卡的左侧增加额外的空间:
注意标签左侧奇怪的空白,我想删除它
选项卡xml:
我在我使用的同一个问题上看到您,您使用的解决方案与我相同,我也有相同的问题!但我发现使用一个自定义的分频器100%解决了这个问题,现在我只在我的两个选项卡中间有一个分频器。这是我正在使用的可拖动:
<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>
你能提供实际的根吗?或者简单地说,你有没有使用过任何边距?我想已经很晚了,但是root只是相对的,没有,我没有提供任何边距真的吗?调用LinearLayout.setShowDividers()时使用了什么?我编辑了我的答案,以显示我正在使用的相同代码,因此它应该适用于您。
<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>
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);
}