Android TabLayout tabPaddingTop and tabPaddingBottom not being removed
请参阅上述问题。
即使我将我的设计库更新为“23.2.0”,Tab布局也搞砸了。
下图是我的标签布局。
Xml部分: -
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="@dimen/dp2"
app:tabMode="fixed"
app:tabSelectedTextColor="@android:color/white"
app:tabTextAppearance="@style/MyCustomTabTextAppearance" />
样式xml: -
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/color_156084</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">@dimen/sp14</item>
<item name="android:textColor">@android:color/white</item>
<item name="textAllCaps">false</item>
</style>
我已将padding设置为-1dp,甚至将tabGravity填充,但没有任何工作。
此代码曾用于早期版本,但现在如果我降级它,我在TintManager上得到一个没有类def找到错误。
设置不同的值或布局参数不起作用,所以我得到的唯一解决方案是在将选项卡添加到选项卡布局后添加以下内容,
final ViewGroup test = (ViewGroup)(tabs.getChildAt(0));//tabs is your Tablayout
int tabLen = test.getChildCount();
for (int i = 0; i < tabLen; i++) {
View v = test.getChildAt(i);
v.setPadding(0, 0, 0, 0);
}
尝试将以下属性添加到TabLayout:
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"
希望它能奏效。
尝试在LinearLayout中添加TabLayout,如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
style="@style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF" />
</LinearLayout>
在Styles.xml中添加:
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">@style/MyCustomTextAppearance</item>
</style>
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
</style>
只需将tabLayout传递给此方法即可完成!
private void setTabLayoutMatchParent(TabLayout tabLayout) {
final ViewGroup tabLayoutChild = (ViewGroup)(tabLayout.getChildAt(0));
int tabLen = tabLayoutChild.getChildCount();
for (int j = 0; j < tabLen; j++) {
View v = tabLayoutChild.getChildAt(j);
v.setPadding(0, 0, 0, 0);
}
}
在我的情况下,问题是给我用于标签的自定义布局固定高度。使用match_parent
解决了我的问题。