简体   繁体   中英

Collapsed Toolbar Title is not centered when setDisplayHomeAsUpEnabled is true

I am using a CollapsingToolbarLayout and when it collapses, the title is not in the middle due to the space occupied by the back button as I set setDisplayHomeAsUpEnabled to true. Any way to fix this?

Take note that I am NOT using a separate TextView for the title, instead I am using the CollapsingToolbarLayout 's title attribute.

在此处输入图片说明

Here is my 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>

First of all, you need to set app:contentInsetStartWithNavigation="0dp" in Toolbar xml to remove margin between icon and text.
Then you should set app:contentInsetEnd="@dimen/icon_width" in Toolbar XML to add margin at the end of text.
In my case height and width of the icon are 56d. It's equal to toolbar height.
Also add app:collapsedTitleGravity="center" in CollapsingToolbarLayout XML.

工具栏示例

The problem is only when the navigationIcon is applied on the toolbar in your case setDisplayHomeAsUpEnabled . When we applying this the toolbar's width is being reduced by the size of BackButton Icon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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