我将tabGravity的TabLayout设置为fill
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@drawable/tab_background"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMaxWidth="0dp"
app:tabMode="fixed" />并希望在选项卡之间添加自定义分隔符。我试过这个方法
View root = tabLayout.getChildAt(0);
LinearLayout linearLayout = (LinearLayout) root;
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(Color.WHITE);
drawable.setSize(5, 0);
linearLayout.setDividerDrawable(drawable);它工作得很好,除了第一个选项卡的开头。我使用的是SHOW_DIVIDER_MIDDLE,但第一个选项卡左边有空白处:


如果我删除分隔符,边距就消失了。如何在保留分隔符的同时去掉空白处?
下面是完整的测试项目:https://github.com/PiN73/TestDivider
发布于 2018-04-23 04:30:45
您可以为每个选项卡膨胀不同的布局。
例如:
private void setupTabs() {
// Left tab inflate
{
LinearLayout tab = (LinearLayout) LayoutInflater.from(mContext)
.inflate(R.layout.tab_left, null);
TextView tvTabTitle = tab.findViewById(R.id.tvTabTitle);
tvTabTitle.setText(getResources().getString(R.string.tab_left));
tabLayout.getTabAt(0)
.setCustomView(tab);
}
// Middle tab inflate
{
LinearLayout tab = (LinearLayout) LayoutInflater.from(mContext)
.inflate(R.layout.tab_middle, null);
TextView tvTabTitle = tab.findViewById(R.id.tvTabTitle);
tvTabTitle.setText(getResources().getString(R.string.tab_middle));
tabLayout.getTabAt(1)
.setCustomView(tab);
}
// Right tab inflate
{
LinearLayout tab = (LinearLayout) LayoutInflater.from(mContext)
.inflate(R.layout.tab_right, null);
TextView tvTabTitle = tab.findViewById(R.id.tvTabTitle);
tvTabTitle.setText(getResources().getString(R.string.tab_right));
tabLayout.getTabAt(2)
.setCustomView(tab);
}
}根据上面的示例,您有3种不同的布局,R.layout.tab_left,R.layout.tab_middle和R.layout.tab_right
您可以将这些布局安排为:
你也可以检查这个问题,它的答案是:How to set the divider between Tabs in TabLayout of design support library?
https://stackoverflow.com/questions/49970254
复制相似问题