android - layout_height ="wrap_content"不适用于 android.support.design.widget.TabLayout

标签 android android-layout android-tabs

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

下面是我的自定义 UI: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"/>

当我增加 TextView 的上述高度时,Tablayout 的高度不会增加,并且选项卡中的自定​​义 View 从底部被截断。

我认为 Tablayout 的高度是如何固定的(可能是操作栏的高度),即使将其高度更改为“match_parent”也不起作用。

如何增加 Tablayout 的高度?我做错了什么吗?

最佳答案

TabLayout 的高度似乎不能低于 48 个 DIP。

查看 TabLayout 的 onMeasure 方法和 how it calculates height.

onMeasure 方法根据来自 TabLayout.DEFAULT_HEIGHT 的硬编码 DIP 值计算“idealHeight” .

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

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

一定要通过super.onMeasure调用,这样这个View的children才能被测量。在调用 tabStrip.getMeasuredHeight()

之前,这很重要
@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());
}

关于android - layout_height ="wrap_content"不适用于 android.support.design.widget.TabLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38115949/

相关文章:

Android CursorIndexOutOfBoundsException 请求索引 0,大小为 0

android - 防止 Cordova Media 插件停止 native Android 应用程序音乐

android - 如何以编程方式从辅助服务中添加和删除布局?

android - 如何将一个 fragment 传递给选项卡菜单中的另一个 fragment ?

带有自定义 View 选项卡的 Android TabLayout - 图标单击不会切换选项卡

android - 如果是手机或平板电脑 (Android 2.1+),则一个 APK 会启动不同的 Activity

android - 由于 ModelClass 在 KotlinMultiplatform 应用程序中不是可序列化或可解析的,导致 nav_graph 参数错误

android - 如何将正方形 View 放入带有圆角 clipcorners 的布局中

Android AppCompat 工具栏不响应触摸操作项

android - 选择 ActionbarSherlock 选项卡时重新创建 fragment