LayoutGravity View внутри панели инструментов не работает должным образом - PullRequest
1 голос
/ 07 июня 2019

Я использую 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 ]

1 голос
/ 07 июня 2019

Попробуйте этот код. Это будет работать !!!

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

0 голосов
/ 07 июня 2019

Некоторые обходные пути, попробуйте заменить панель инструментов ниже

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

Дай мне знать, сработало ли ты как хочешь

0 голосов
/ 07 июня 2019

попробуйте использовать атрибут гравитации, как этот андроид: 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>
...