Android 表格布局中的意外余量
我已将tabGravity设置为Android 表格布局中的意外余量,android,Android,我已将tabGravity设置为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="fi
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" />
除了第一个选项卡的开头,它工作正常。我使用的是SHOW\u DIVIDER\u MIDDLE
,但第一个选项卡的左边有边距:
如果我去掉分隔符,边距就会消失。如何去掉边距但保留分隔符
这是完整的测试项目:您可以为每个选项卡膨胀不同的布局 例如:
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\u left,R.layout.tab\u middle和R.layout.tab\u right
您可以将这些布局安排为:
- R.layout.tab_左:文本视图+分隔符
- R.layout.tab_middle:文本视图
- 右布局选项卡:分隔符+文本视图
您也可以检查这个问题,答案是:谢谢您的回答。是否可以对自定义视图使用选择器?我想使活动选项卡与众不同color@Pavel是的,您还可以为选项卡的背景和每个自定义选项卡tayout中的文本提供任何颜色或可绘制选择器。
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);
}
}