【问题标题】:Collapsed Toolbar Title is not centered when setDisplayHomeAsUpEnabled is true当 setDisplayHomeAsUpEnabled 为 true 时,折叠的工具栏标题不居中
【发布时间】:2020-04-20 19:29:20
【问题描述】:

我正在使用CollapsingToolbarLayout,当它折叠时,由于我将setDisplayHomeAsUpEnabled 设置为true 时后退按钮占用的空间,标题不在中间。有什么办法解决这个问题?

请注意,我使用单独的TextView 作为标题,而是使用CollapsingToolbarLayouttitle 属性。

这是我的 XML:

    <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:fitsSystemWindows="true"
        app:collapsedTitleGravity="center_horizontal"
        app:contentScrim="@color/main_orange"
        app:expandedTitleMarginBottom="75dp"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="10dp"
        app:expandedTitleTextAppearance="@style/CollapsibleToolbarTitleTextAppearanceExpanded"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_margin="5dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_image_area_black_48dp"
                    android:tint="@color/white_50" />

                <TextView
                    android:id="@+id/failedToLoadImageText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:shadowColor="@color/black"
                    android:shadowRadius="3"
                    android:text="Please connect to the internet to view the photo"
                    android:textColor="@color/white_50" />
            </LinearLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/galleryViewPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_alignParentBottom="true"
                android:layout_centerVertical="true"
                android:background="@drawable/black_gradient"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

        </RelativeLayout>

        <TextView
            android:id="@+id/subtitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="30dp"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:maxLines="2"
            android:text="Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran Lorem Ipsum Iran "
            android:textColor="@android:color/white"
            android:textSize="16sp"
            app:layout_collapseMode="parallax" />

        <com.rd.PageIndicatorView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|bottom"
            android:layout_marginBottom="10dp"
            app:layout_collapseMode="parallax"
            app:piv_radius="5dp"
            app:piv_selectedColor="@color/main_orange"
            app:piv_unselectedColor="@color/dim_gray_50"
            app:piv_viewPager="@id/galleryViewPager" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:contentInsetEnd="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

【问题讨论】:

  • 如您所见,我没有使用单独的TextView 作为标题。我正在使用CollapsingToolbarLayout 的标题。
  • @DaleJulian 你为什么不使用文本视图作为标题。如果它解决了你的问题,你应该使用它。 ?
  • 由于我使用的是CollapsingToolbarLayout,所以我还希望标题具有动画效果。
  • 你试过把 android:layout_centerInParent="true" 给 failedToLoadImageText TextView 吗?

标签: android android-layout android-collapsingtoolbarlayout android-appbarlayout


【解决方案1】:

首先,您需要在Toolbar xml中设置app:contentInsetStartWithNavigation="0dp",以去除图标和文本之间的边距。
然后您应该在 Toolbar XML 中设置 app:contentInsetEnd="@dimen/icon_width" 以在文本末尾添加边距。
在我的情况下,图标的高度和宽度是 56d。它等于工具栏高度。
同时在 CollapsingToolbarLayout XML 中添加app:collapsedTitleGravity="center"

【讨论】:

    【解决方案2】:

    问题仅在于 navigationIcon 应用于您的情况 setDisplayHomeAsUpEnabled 的工具栏上。 当我们应用这个时,工具栏的宽度会被 BackButton Icon 的大小减小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 2016-02-01
      相关资源
      最近更新 更多