【问题标题】:How to use Collapsing Toolbar with TabLayout using Material and AndroidX components?如何使用 Material 和 AndroidX 组件将折叠工具栏与 TabLayout 结合使用?
【发布时间】:2019-05-26 01:38:44
【问题描述】:

我正在尝试将 Toolbar 和 TabLayout 与 CollapsingToolbarLayout 一起使用,但标签布局出现了非常奇怪的行为。这个想法是让 TabLayout 在 Toolbar 展开时具有透明背景,并且在折叠时,像任何其他具有 Toolbar 和 TabLayout 的 Activity 一样位于 Toolbar 下方。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
    android:layout_width="match_parent"
    android:layout_height="250dp">

    <com.google.android.material.appbar.CollapsingToolbarLayout
      android:id="@+id/collapsing_toolbar_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_scrollFlags="scroll|exitUntilCollapsed"
      app:contentScrim="@color/primary"
      app:expandedTitleTextAppearance="@style/CollapsingToolbarExpandedTitleTheme">

      <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@null"
        android:scaleType="centerCrop"
        app:layout_collapseMode="parallax"/>

      <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        app:titleTextAppearance="@style/ToolbarTitleTheme"
        app:layout_collapseMode="pin"/>

      <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        android:layout_gravity="bottom">

        <com.google.android.material.tabs.TabItem
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>

        <com.google.android.material.tabs.TabItem
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>

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

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

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

  <androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.viewpager.widget.ViewPager
      android:id="@+id/view_pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

  </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

更新:按照@ismail alaoui (https://android.jlelse.eu/parallax-scrolling-header-tabs-android-tutorial-2cc6e40aa257) 提到的示例,布局更好,但 TabLayout 仍然是白色的。没有与之关联的样式。

更新:按照@ismail 的建议,将 android:background="transparent" 添加到 TabLayout 可以解决此问题。我还在 TabLayout (Widget.MaterialComponents.TabLayout.Colored) 中添加了一个样式,只是为了改进布局。

【问题讨论】:

    标签: android android-layout android-toolbar android-collapsingtoolbarlayout androidx


    【解决方案1】:

    我建议你关注this 教程,它解释了如何做你正在寻找的东西,它没有使用 androidx 组件,但你可以以同样的方式使用它,享受!

    编辑 :

    将此添加到appBarLayout 和TabLayout 属性:

    android:background="@android:color/transparent"
    

    【讨论】:

    • 我现在做了,布局更好了,但是 TabLayout 继续变白。
    • 检查你的tablayout风格
    • 没有与 TabLayout 关联的样式。它与示例完全相同。
    • 是的,我正在更新问题。
    • 将此添加到 appbarlayout android:background="@android:color/transparent">
    猜你喜欢
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多