android.support.design.widget.TabLayout中的layout_height="wrap_content"无法正常工作

4
 <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/primary_color"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/AppTheme"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabIndicatorColor="@color/red"
            app:tabIndicatorHeight="3dp"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_below="@+id/app_bar_layout"/>

</android.support.design.widget.CoordinatorLayout>

我已经设置了选项卡的自定义UI:

private TabLayout mTabLayout;
for (int i = 0; i < TITLES.length; i++) {
        RelativeLayout tabView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        mTabLayout.getTabAt(i).setCustomView(tabView);
}

以下是我的自定义用户界面:custom_tab.xml
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="Title"
    android:textSize="14sp"/>

当我增加文本视图的高度时,选项卡布局的高度不会增加,而选项卡中的自定义视图将从底部被裁剪。

我想选项卡布局的高度是固定的(可能是操作栏的高度),即使将其高度更改为“match_parent”也无效。

如何增加选项卡布局的高度?我做错了什么吗?


1
尝试将TabLayout的实际高度设置为60dp。 - naiyu
谢谢。之前,我将高度设置为“match_parent”,但不起作用。但是将其设置为60dp或100dp后,高度会改变。奇怪的问题。 - Shanki Bansal
1个回答

3

TabLayout似乎被限制为高度不小于48 DIP。

看一下TabLayout的onMeasure方法以及它如何计算高度。

onMeasure方法根据TabLayout.DEFAULT_HEIGHT中硬编码的DIP值计算出“idealHeight”。

由于这是一个Material Design小部件布局,Google可能想确保您不违反他们的Material Design规范,关于某些重要人物制定的可触摸高度规则 :-P

我通过子类化TabLayout并将其高度设置为与第一个子项(tabStrip)完全匹配的高度来创建了一个解决方法。

请务必在调用tabStrip.getMeasuredHeight()之前通过super.onMeasure调用此视图的子项进行测量。这是非常重要的。

@NonNull private ViewGroup tabStrip;

public CustomTabLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    tabStrip = (ViewGroup) getChildAt(0);
    tabStrip.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // Force the height of this ViewGroup to be the same height of the tabStrip
    setMeasuredDimension(getMeasuredWidth(), tabStrip.getMeasuredHeight());
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接