首页 > 解决方案 > 更改选定的 TabItem 高度

问题描述

我正在使用 TabLayout 并且我正在寻找一种方法来更改所选 TabItem 的高度,所以这个会比其他的要大。为此,我尝试使用 TabIndicator,但我只能更改它的颜色和大小,而不是它的位置(它总是在 TabLayout 内,我需要将它放在它的正上方以达到我需要的效果)。

有没有办法使用选择器或在布局 xml 本身中实现这一点?

下面是所需效果的示例(左侧是选定的选项卡):

在此处输入图像描述

这是我到目前为止的代码:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:tabIndicatorHeight="0dp"
            app:tabBackground="@drawable/nav_item_color_state">
                <android.support.design.widget.TabItem
                    android:id="@+id/tab1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Tab 1"/>
                <android.support.design.widget.TabItem
                    android:id="@+id/tab2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Tab 2"/>
        </android.support.design.widget.TabLayout>

</android.support.constraint.ConstraintLayout>

标签: androidxmllayouttabs

解决方案


我最终使用了两个按钮并创建了两个不同的可绘制对象,以在其背景中用作选择/未选择状态。一点也不优雅,但它奏效了。


推荐阅读