【问题标题】:CollapsingToolbarLayout center title not workingCollapsingToolbarLayout 中心标题不起作用
【发布时间】:2018-05-09 21:06:30
【问题描述】:

我正在使用工具栏制作折叠动画。 我发现我的工具栏标题不在标题栏中。

我尝试使用 collapsedTitleGravity 属性。 但是失败了。

我的错误是什么? 我希望有人知道为什么会这样。

这是我的代码布局。

<android.support.design.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">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="130dp"
        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="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleMarginStart="@dimen/activity_horizontal_margin"
            android:fitsSystemWindows="true"
            app:collapsedTitleGravity="center_horizontal"
            app:title="@string/activity_title_complete_request">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:background="@drawable/rectangle9_1"
                android:id="@+id/profile_id"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin">

            </android.support.v7.widget.Toolbar>

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="10dp">

            <include layout="@layout/fragment_commercial_check_list" />

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

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

非常感谢。

【问题讨论】:

  • 为什么不在折叠收费栏内采用另一个相对布局并为该视图设置重力

标签: android xml layout


【解决方案1】:

您使用边距设置您的标题。但是使用边距您不能设置中心。请检查下面的代码。我认为你的标题将是中心。

<android.support.design.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">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="130dp"
    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="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:collapsedTitleGravity="center_horizontal">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:background="@drawable/rectangle9_1"
            android:id="@+id/profile_id"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:layout_collapseMode="pin">

            <TextView
                android:layout_width="match_parent"
                android:gravity="center"
                android:layout_height="wrap_content" 
                android:text="Title"/>

        </android.support.v7.widget.Toolbar>

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

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="10dp">

        <include layout="@layout/fragment_commercial_check_list" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

【讨论】:

  • 谢谢。但正如你可以看到我的代码,我想用折叠动画显示标题。如果您使用您的代码,我无法应用折叠动画。
【解决方案2】:

尝试改变这些属性的值

 app:expandedTitleMarginEnd="64dp"
 app:expandedTitleMarginStart="64dp"

并添加这个

app:expandedTitleGravity="center_horizontal"
app:collapsedTitleGravity="center_horizontal"

【讨论】:

  • 感谢您的回复,但它不起作用。如您所见,我希望看到向左扩展不是居中。
猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 2021-08-26
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多