java - android - 带有 tablayout 设置背景的折叠工具栏

标签 java android

我的应用程序包含一个带有选项卡 View 的折叠工具栏。选项卡有自己的背景颜色,我的@color/colorPrimary,折叠工具栏有一个设置的背景图像。现在我想“合并”它们:选项卡布局应该是透明的,因此背景图像从工具栏向下延伸,包括选项卡。就像它不再被背景分割一样。 我设置了布局并尝试使选项卡透明,但这样图像将无法继续。

我的 XML 文件:

 <android.support.design.widget.CoordinatorLayout  
    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"
    android:fitsSystemWindows="true"
    tools:context="com.example.te.e6.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/colbar"
            android:layout_width="match_parent"
            android:layout_height="175dp"
            app:expandedTitleGravity="center|bottom"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/materialbackground"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:expandedTitleGravity="center|bottom"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            app:layout_anchor="@id/appBar"
            app:layout_anchorGravity="bottom">

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="A" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="B" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="C" />

        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

最佳答案

我也一直在寻找解决方案,我偶然发现了这种方法,这对我有一些帮助。最后我成功了。这里我使用最新的Material3发布我的解决方案:

<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:id="@+id/list_appbarlayout_tabs">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/list_collapsing_toolbar_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|enterAlways|snap">

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

                    <com.google.android.material.tabs.TabItem
                        android:id="@+id/tab1"
                        android:layout_width="wrap_content"
                        android:layout_height="20dp" />

                    <com.google.android.material.tabs.TabItem
                        android:id="@+id/tab2"
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"/>

                    <com.google.android.material.tabs.TabItem
                        android:id="@+id/tab3"
                        android:layout_width="wrap_content"
                        android:layout_height="40dp" />

                </com.google.android.material.tabs.TabLayout>
            </com.google.android.material.appbar.MaterialToolbar>
        </com.google.android.material.appbar.AppBarLayout>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <ImageView
                android:id="@+id/list_background"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="0.2"
                android:padding="24dp"
                app:srcCompat="@drawable/bg" />

            <com.google.android.material.progressindicator.LinearProgressIndicator
                android:id="@+id/list_progress_indicator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="true" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/list_progress_indicator"
                android:layout_marginEnd="8dp">


            </androidx.recyclerview.widget.RecyclerView>


        </RelativeLayout>

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/list_bottom_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:hideOnScroll="true"
            app:menu="@menu/bottombar"
            />

        
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/list_fab_add_contentdesc"
            app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
            app:layout_anchor="@id/list_bottom_bar"
            app:srcCompat="@drawable/ic_add"
            />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

特别注意

 app:layout_scrollFlags="scroll|enterAlways|snap">

对于 Material 工具栏

 app:layout_behavior="@string/appbar_scrolling_view_behavior">

相对布局!

编辑: 菜单开头有一个愚蠢的边距,要消除它,请添加 app:contentInsetStart="0dp"默认设置为 16 dp(还更新了上面的布局)

关于java - android - 带有 tablayout 设置背景的折叠工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49036801/

相关文章:

java - FindBugs Dead Store Warning - 我应该重组代码还是忽略警告

java - 连接到 Amazon RDS Oracle 实例时如何处理 "Got minus one from a read call"错误

android - 从拍摄的照片中放入 ImageView 的图像并不总是显示

android - 折叠工具栏默认折叠

android - 录制 fragment 或 View ,输出视频

java - 无法设置 JTextPane/JScrollPane 的大小

java - 如何在 OSGi 中通过 HttpService 发布 JAX-WS

android - Activity 组子 Activity 中的日期选择器引发 BadTokenException 错误

Java - 从视频中提取关键帧

Android:撤消应用程序数据目录上的 chmod