【问题标题】:LayoutGravity of View inside Toolbar is not working properly工具栏内视图的 LayoutGravity 无法正常工作
【发布时间】:2019-06-07 14:21:37
【问题描述】:

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

<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

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

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

为什么我在使用 CollapsingToolbarLayout 时会出现这种行为?

【问题讨论】:

  • 您可以在 Toolbar 内放置一个 ConstraintLayout。在此 ConstraintLayout 中,根据需要放置 TextView(借助约束)。

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


【解决方案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>

【讨论】:

  • 我必须让它动态化。例如,当我有后退按钮时,设置 android:layout_width="match_parent" 将不起作用
  • 已更新。请检查
【解决方案2】:

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

<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>

让我知道它是否如你所愿

【讨论】:

  • 我必须让它动态化。例如,当我有 后退按钮 时,设置 android:layout_width="match_parent" 将不起作用。
【解决方案3】:

尝试使用像这样的重力属性 android:gravity="center"

            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:gravity="center"
                android:layout_gravity="center"/>
        </android.support.v7.widget.Toolbar>

【讨论】:

    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多