android - 折叠工具栏标题未居中

标签 android android-layout

我的应用程序包含一个带有标题的折叠工具栏。标题在展开时居中,在折叠时保持居中。使用下面的布局,我的设备在折叠时不会将标题居中,而是将其稍微向右移动。我必须改变什么才能让它保持居中?如果有帮助,我当然也可以添加图像。

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/white"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleGravity="center|bottom"
            app:title="Test"
            app:collapsedTitleGravity="center"
            app:expandedTitleMarginBottom="56dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_marginBottom="48dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </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:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:fillViewport="true"
        android:layout_gravity="fill_vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">>

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

This is what it looks like expanded

As you see on this image, it's not centered, but a little moved to the right side

最佳答案

这是因为右上角的后退按钮。解决方案是在工具栏中添加 marginEnd。

<com.google.android.material.appbar.CollapsingToolbarLayout
    android:id="@+id/htab_collapse_toolbar"
    android:layout_width="match_parent"
    android:layout_height="110dp"
    app:expandedTitleMargin="@dimen/margin_15"
    app:collapsedTitleTextAppearance="@style/test1"
    app:expandedTitleTextAppearance="@style/test"
    app:collapsedTitleGravity="center_horizontal"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    android:fitsSystemWindows="true"
    app:title="Send money">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/htab_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_marginEnd="85dp"
        android:layout_gravity="top"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.CollapsingToolbarLayout>

关于android - 折叠工具栏标题未居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910013/

相关文章:

Android:在 Android 2.2 上使用 webview 查看 IP Camera 的图像

java - 安卓工作室 : How to prevent up button in toolbar from pushing child view to the right?

android - 在自定义 AdapterView 中实现 onMeasure() 和 onLayout() 的正确方法

android - 如何使用 Xamarin/Monodroid 设置自定义标题

java - 如何创建迷你对话框(附图)

android - 找不到 com.android.support :multidex-instrumentation:27. 1.1

android - 如何检查 IP 地址是否可达

android - 如何动态更改 MenuItem 的可见性?

android - 有没有办法避免 android 布局上的尺寸规范?

android - 如何更改自定义警报对话框的背景