【问题标题】:Android Tab Layout not taking up full width with custom viewAndroid选项卡布局不占用自定义视图的全宽
【发布时间】:2016-06-21 23:35:57
【问题描述】:

Android TabLayout tabPaddingTop and tabPaddingBottom not being removed

请同时参考上述问题。

即使我将我的设计库更新为“23.2.0”,标签布局也一团糟。

下图是我的标签布局。

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>

我已将填充设置为 -1dp,甚至使用 tabGravity 进行填充,但没有任何效果。

这段代码曾经在早期版本中工作,但现在如果我降级它,我会在 TintManager 上收到 no class def found 错误。

【问题讨论】:

  • 你能发布整个xml文件吗
  • 标签内部有填充,。 xml的其他部分对其没有任何影响
  • @RahulGupta 你是如何解决这个问题的?我面临同样的问题。选项卡占用了额外空间并适合全屏显示。

标签: android android-tablayout androiddesignsupport


【解决方案1】:

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

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"
    

    希望它会起作用。

    【讨论】:

      【解决方案3】:

      您可以直接访问TabLayout.Tab 视图并将填充设置为0,而不是处理TabLayout 子视图,就像下面的代码一样。

      for (int i = 0; i < tabLayout.getTabCount(); i++) {
          View tabView = tabLayout.getTabAt(i).view;
          tabView.setPadding(0, 0, 0, 0);
      }
      

      我在onCreateView 回调中执行此操作,并且效果很好。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 这对我来说效果很好,谢谢!
        【解决方案5】:

        我被困了好几个小时,直到找到方法tab.setCustomView(idRes)。从膨胀到这对我有用。或者,如果您正在膨胀,您可以使用 TabLayout.TabView 作为根视图组。

        除此之外,我不确定它是否有帮助,但我使用minWidth 来自定义视图布局。

        这就是我wanted

        【讨论】:

        • 通过执行 setCustomView(idRes),布局参数被正确解释,因为膨胀的视图直接链接到根视图。这节省了我很多时间。谢谢你!
        【解决方案6】:

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

        【讨论】:

        • 你也在使用浏览器吗?
        • 是的,但查看寻呼机对此没有任何影响。
        【解决方案7】:

        在我的情况下,问题是为我用于选项卡的自定义布局提供 固定高度。 使用 match_parent 解决了我的问题。

        【讨论】:

          【解决方案8】:

          TabLayout 中有属性:app:tabPaddingStart 和 app:tabPaddingEnd

          您可以将它们都设置为-1,以便在 TabLayout 的自定义视图中删除填充开始和结束。

          <com.google.android.material.tabs.TabLayout
                  android:id="@+id/tlEmojis"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  app:tabPaddingStart="-1dp"
                  app:tabPaddingEnd="-1dp"
                  app:tabMaxWidth="@dimen/dimen_48dp"
                  app:tabMode="scrollable" />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-09-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-15
            相关资源
            最近更新 更多