使用CoordinatorLayout时,操纵栏标题不会垂直对齐

时间:2017-11-19 07:17:28

标签: android android-actionbar android-coordinatorlayout

所以我有一个带有CoordinatorLayout的片段如下:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.susheel.pocketparliament.fragments.pages.HomeFragment">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="350dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@color/colorPrimary"
            app:expandedTitleTextAppearance="@color/colorAccent"
            android:fitsSystemWindows="true"
            app:collapsedTitleGravity="top">

            <ImageView
                android:src="@drawable/building_1"
                app:layout_collapseMode="parallax"
                android:scaleType="centerCrop"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin"
                app:title="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:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lorem"
                android:textStyle="bold"
                android:padding="12dp"
                android:textSize="20sp"/>
        </LinearLayout>

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

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

这是我的MainActivity的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.susheel.pocketparliament.MainActivity">

    <!--This will be replaced with fragments-->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/sidenav_menu"
        app:headerLayout="@layout/navigation_header">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

使用FragmentManager添加片段以替换FrameLayout。但是,当我向上滚动CoordinatorLayout时,我的ActionBar上的标题不会垂直居中。这就是:

enter image description here

enter image description here

如何将操作栏标题设为中心?谢谢!

编辑:抱歉这些巨大的图片

2 个答案:

答案 0 :(得分:0)

据我所知,没有任何简单的方法可以做到这一点,但这可能会有所帮助: How to center align the ActionBar title in Android?

答案 1 :(得分:0)

只需在工具栏中使用android:paddingBottom =“ 10dp”。

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