工具栏内的视图的LayoutGravity无法正常工作

时间:2019-06-07 03:41:15

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

我正在使用CollapsinToolbarLayout,并且在TextView的内部包含了一个工具栏Toolbar,并希望使其居中。

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <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:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        android:fitsSystemWindows="true">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Title"
                android:layout_gravity="center"/>
        </android.support.v7.widget.Toolbar>

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

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

从上面可以看出,我设置了android:layout_gravity="center"并期望将TextView居中放在Toolbar

但是,这是我得到的结果。

enter image description here

我在我的其他非View中使用CollapsingToolbarLayout,并且它们根据我设置的重力进行显示,但我无法使其与CollapsingToolbarLayout一起使用。因此,我想它与CollapsingToolbarLayout或其父AppBarLayout有关,但无法弄清楚是什么。

为什么在使用CollapsingToolbarLayout时出现这种现象?

3 个答案:

答案 0 :(得分:1)

尝试此代码。它将起作用!!!

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
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"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginEnd="64dp"
    app:expandedTitleMarginStart="48dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        app:layout_collapseMode="parallax" />

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


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/image_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:contentDescription="@string/app_name"
                android:src="@drawable/back_icon" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Title"
                android:textColor="@color/white" />

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

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

答案 1 :(得分:0)

尝试使用重力属性,例如android:gravity =“ center”

[
    {'Name': 'test1', 'Email': 'test1@gmail.com', '1000': '', '5000': '', '10000': '', 'Reason': ''},
    {'Name': 'test2', 'Email': 'test2@gmail.com', '1000': '', '5000': '', '10000': '', 'Reason': ''}
]

答案 2 :(得分:0)

一些解决方法,请尝试用下面的工具栏替换工具栏

<android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title"
                    android:layout_marginRight="16dp"
                    android:layout_marginEnd="16dp"
                    android:gravity="center"/>
            </android.support.v7.widget.Toolbar>

让我知道它是否按您的意愿工作

相关问题