【问题标题】:CollapsingToolbarLayout and TabLayoutCollapsingToolbarLayout 和 TabLayout
【发布时间】:2015-08-18 00:15:01
【问题描述】:

我正在尝试创建一个具有带有图像和工具栏的 CollapsingToolbarLayout 的 Activity(就像在 cheesesquare 示例 here 中的 CheeseDetailActivity 中一样),它下面还有一个选项卡布局。

有什么想法可以实现吗?

当尝试将其添加到 CollapsingToolbarLayout 或 AppBarLayout 时,结果是选项卡布局在屏幕顶部

【问题讨论】:

    标签: android android-design-library


    【解决方案1】:

    试试这个结构:

    <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.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.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="SPECIFIC HEIGHT HERE!"
            android:fitsSystemWindows="true"
            android:theme="ADD A STYLE HERE IF YOU WANT">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                >
    
                <ImageView
                    android:id="@+id/backdrop"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:src="YOUR SOURCE"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="YOUR MULTIPLIER"
                    />
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:popupTheme="YOUR POPUP THEME">
    
                </android.support.v7.widget.Toolbar>
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    app:tabGravity="YOUR TAB GRAVITY"
                    app:tabIndicatorColor="YOUR TAB INDICATOR COLOR"
                    app:tabMode="YOUR TAB MODE">
    
                </android.support.design.widget.TabLayout>
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
        </android.support.design.widget.AppBarLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    重要的属性是TabLayout的layout_gravitybottom

    对于 API 21 及更低版本,我遇到了 TabLayout 消失的问题。如果你遇到同样的问题,对于 TabLayout(对于低于 21 的 API)使用这个:

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:translationY="YOU HAVE TO EXPERIMENT WITH THIS ATTRIBUTE - (in dps)"
        app:tabGravity="YOUR TAB GRAVITY"
        app:tabIndicatorColor="YOUR TAB INDICATOR COLOR"
        app:tabMode="YOUR TAB MODE"
        >
    

    您必须根据您为应用栏提供的大小来试验translationY 属性。您将在 dps 中输入一个值,并在几分钟内确定它。

    希望它对你有用,因为它对我有用!

    【讨论】:

    • 我在示例中添加了 ViewPager,因为 a) TabLayout 需要它,b) 它将采用 layout_behavior="@string/appbar_scrolling_view_behavior"
    • 嘿,您是否将它与具有重力填充的固定选项卡一起使用?我只有两个标签,希望它们填满整个宽度,但它们总是会居中显示,宽度由最长的标签决定......对此有什么想法吗?
    • 我没有时间尝试更多。如果我有任何结果,我会尽快回复您
    • 如何将tablayout放在actionbar下方?
    • @PeiyongYang 你能发一个链接到一个模型,以便了解你想要实现的目标吗?
    【解决方案2】:

    另见sample,它解决了同样的问题。

    在 API 14-23 上测试。工作没有任何问题。

    【讨论】:

    • 谢谢,花了很多时间寻找可行的解决方案,这正是我所需要的。
    【解决方案3】:
    <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">
    
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="230dp"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/header"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/anim_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:gravity="top"
                    android:minHeight="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:titleMarginTop="15dp" />
    
                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="bottom" />
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
        </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>
    
    <android.support.design.widget.NavigationView
    
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/header" />
    

    试试这个...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-10
      • 2016-02-18
      • 1970-01-01
      • 2016-05-15
      • 2015-08-21
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      相关资源
      最近更新 更多