简体   繁体   English

如何在折叠工具栏中将文本视图转换为标题?

[英]How to have a textview transition to the title in a collapsing toolbar?

I have a CollapsingToolbarLayout that contains a TextView and a RatingBar under the TextView . 我有一个CollapsingToolbarLayout ,它包含TextView下的TextViewRatingBar Would it be possible to have the TextView somehow transition to the title? 是否有可能让TextView以某种方式过渡到标题?

My current layout looks like this: 我目前的布局如下: 在此输入图像描述

I want "Title" to transition up as the toolbar collapses. 我希望“标题”在工具栏折叠时转换。 There will also be a back button, so how would I ensure that the textview transitions to the proper place (to the right side of the back button)? 还会有一个后退按钮,那么我如何确保textview转换到正确的位置(后退按钮的右侧)?

EDIT: Here is my XML 编辑:这是我的XML

<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
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"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_view_app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedTextStyle"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginLeft="@dimen/double_margin"
            android:gravity="center_vertical"
            android:orientation="vertical"
            app:layout_collapseMode="parallax">

            <TextView
                android:id="@+id/title"
                fontPath="fonts/OpenSans-Bold.ttf"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Title"
                android:textColor="@android:color/white"
                android:textSize="@dimen/large_text"/>

            <RatingBar
                android:id="@+id/rating_bar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

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

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

The solution is: Don't use a TextView . 解决方案是:不要使用TextView

You can position the expanded title wherever you want by using these CollapsingToolbarLayout attributes: 您可以使用以下CollapsingToolbarLayout属性将展开的标题放在任何位置:

app:expandedTitleGravity        default is bottom|left -or- bottom|start
app:expandedTitleMargin
app:expandedTitleMarginBottom
app:expandedTitleMarginStart
app:expandedTitleMarginEnd

I am assuming you are using a NoActionBar theme, which is why you started off with the TextView for the title. 我假设您正在使用NoActionBar主题,这就是为什么您开始使用TextView作为标题。

So here is your layout, fixed up to use the CollapsingToolbarLayout title: 所以这是你的布局,修复使用CollapsingToolbarLayout标题:

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    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="@dimen/detail_view_app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedTextStyle"
            app:expandedTitleMarginBottom="64dp"
            app:expandedTitleMarginStart="16dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <!-- 
                 set the expandedTitleMarginBottom to a value 
                 that positions the expanded title above the rating bar
              -->

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

            <RatingBar
                android:id="@+id/rating_bar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginLeft="16dp"
                app:layout_collapseMode="parallax" />

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

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

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

Then in your code, set the title on the CollapsingToolbarLayout : 然后在您的代码中,在CollapsingToolbarLayout上设置标题:

        CollapsingToolbarLayout collapsingToolbar =
                (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
        collapsingToolbar.setTitle("Title");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM