滚动图片和appbar标题,但在Android中保留标签

时间:2016-05-04 17:01:34

标签: android android-coordinatorlayout

我使用应用支持和CoordinatorLayout。我能够实现CollapsingToolbarLayoutImageViewToolbar在滚动时隐藏。我对标签很困难 - TabLayout没有崩溃,这很好。问题是它看起来不太好 - 我希望它使用图片的底部作为背景。

我在CollapsingToolbarLayout内移动了TabLayout,然后它将图片用作背景,但有两个问题 - 标签位于屏幕顶部,滚动时隐藏它们。

enter image description here

我希望达到它将位于图片底部的效果

enter image description here

当其他部分滚动时,它将保持在屏幕顶部

enter image description here

布局:

<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.design.widget.AppBarLayout
    android:id="@+id/main.appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true"
    >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="4dp"
        app:expandedTitleMarginEnd="64dp"
        >

        <ImageView
            android:id="@+id/main.backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            android:src="@drawable/digit1_hill"
            app:layout_collapseMode="parallax"
            />

        <android.support.v7.widget.Toolbar
            android:id="@+id/main.toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"
            />

        <android.support.design.widget.TabLayout
            android:id="@+id/main.tabs"
            android:layout_width="fill_parent"
            android:layout_height="?attr/actionBarSize"
            app:tabSelectedTextColor="?android:attr/textColorPrimaryInverse"
            app:tabIndicatorColor="?android:attr/textColorPrimaryInverse"
            app:tabIndicatorHeight="4dp"
            />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

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

1 个答案:

答案 0 :(得分:2)

您的Toolbar应该app:layout_scrollFlags="scroll|enterAlways"

,您需要告诉CollapsingToolbarLayout app:layout_scrollFlags="scroll|exitUntilCollapsed"TabLayout android:layout_gravity="bottom" <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.design.widget.AppBarLayout android:id="@+id/main.appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:fitsSystemWindows="true"> <!-- Fit the system windows for your CollapsingToolbarLayout also set your height to wrap_content and give image a height--> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/main.collapsing" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginStart="4dp" app:expandedTitleMarginEnd="64dp"> <ImageView android:id="@+id/main.backdrop" android:layout_width="match_parent" android:layout_height="244dp" android:scaleType="centerCrop" android:fitsSystemWindows="true" android:src="@drawable/digit1_hill" app:layout_collapseMode="parallax"/> <!-- Tell your toolbar to scroll|enterAlways --> <android.support.v7.widget.Toolbar android:id="@+id/main.toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_scrollFlags="scroll|enterAlways"/> <!-- add layout_gravity="bottom" --> <android.support.design.widget.TabLayout android:id="@+id/main.tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" app:tabSelectedTextColor="?android:attr/textColorPrimaryInverse" app:tabIndicatorColor="?android:attr/textColorPrimaryInverse" app:tabIndicatorHeight="4dp" android:layout_gravity="bottom"/> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/dashboard.viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </android.support.design.widget.CoordinatorLayout>

试试这个:

<style name="NewAppTheme" parent="Theme.AppCompat.Light.NoActionBar">

   <!-- Include all items from original theming -->

</style>

<强>更新 此外,您应该切换主题或添加新主题:

<style name="Invisible" parent="AppTheme"> 
    <item name="android:textColor">@android:color/transparent</item> 
</style>

并隐藏标题创建样式:

app:collapsedTitleTextAppearance="@style/Invisible"

然后将其设置为collapsedTitleTextAppearance:

{{1}}