Android标签布局不占用自定义视图的全宽度

问题描述 投票:6回答:5

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找到错误。

enter image description here

android android-tablayout androiddesignsupport
5个回答
7
投票

设置不同的值或布局参数不起作用,所以我得到的唯一解决方案是在将选项卡添加到选项卡布局后添加以下内容,

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);
        }

2
投票

尝试将以下属性添加到TabLayout:

app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"

希望它能奏效。


0
投票

尝试在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>

0
投票

只需将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);
    }
}

0
投票

在我的情况下,问题是给我用于标签的自定义布局固定高度。使用match_parent解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.