【发布时间】: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