Tab指示器不再位于AppBarLayout的底部。该选项卡是定制的选项卡

问题描述 投票:1回答:1

我将android应用迁移到AndroidX,然后注意到了这一点

enter image description here

您可以看到选项卡指示器进入了AppBarLayout。这是代码

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:elevation="4dp">

    <androidx.appcompat.widget.Toolbar
        ... // some code here relevant to toolbar
    </androidx.appcompat.widget.Toolbar>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/custom_tab" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/custom_tab" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/custom_tab"/>

    </com.google.android.material.tabs.TabLayout>

</com.google.android.material.appbar.AppBarLayout>

我创建的自定义布局如下:

    <?xml version="1.0" encoding="utf-8"?>
       <androidx.constraintlayout.widget.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="wrap_content"
        android:layout_height="wrap_content">

      <TextView
       android:id="@+id/tab_title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       tools:text="CONTRIBUTIONS"
       style="@style/TextAppearance.Design.Tab"/>

       <TextView
       android:id="@+id/tv_number"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintTop_toBottomOf="@id/tab_title"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       tools:text="1000"
       android:layout_marginBottom="8dp"
       style="@style/TextAppearance.Design.Tab"
       />

       <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginStart="8dp"
       android:layout_marginLeft="8dp"
       android:layout_marginEnd="4dp"
       android:layout_marginRight="4dp"
       android:text="@string/open_bracket"
       app:layout_constraintBottom_toBottomOf="@+id/tv_number"
       app:layout_constraintEnd_toStartOf="@+id/tv_number"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="@+id/tv_number"
       app:layout_constraintHorizontal_bias="1"
       style="@style/TextAppearance.Design.Tab"/>

       <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginStart="4dp"
       android:layout_marginLeft="4dp"
       android:layout_marginEnd="8dp"
       android:layout_marginRight="8dp"
       android:text="@string/closing_bracket"
       app:layout_constraintBottom_toBottomOf="@+id/tv_number"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toEndOf="@+id/tv_number"
       app:layout_constraintTop_toTopOf="@+id/tv_number"
       app:layout_constraintHorizontal_bias="0"
       style="@style/TextAppearance.Design.Tab"/>
    </androidx.constraintlayout.widget.ConstraintLayout>`

[在android 22及以下版本中看起来不错,但在android 24及以上版本中则没有。但是,在迁移到androidx之前,它在所有版本中都很好,即指标位于appbarlayout的底部而不是其中。

android-studio androidx android-tablayout android-appbarlayout
1个回答
1
投票

您必须像这样在TabLayout中设置app:tabIndicatorColor="YOUR_COLOR"

<com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@color/blue">

... // Your TabItems

</com.google.android.material.tabs.TabLayout>

如果不起作用,也尝试从TabItems中删除android:layout="@layout/custom_tab"

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