Название панели инструментов не центрируется при использовании меню - PullRequest
0 голосов
/ 16 апреля 2020

Моя панель инструментов содержит 3 компонента.

  1. Левое изображение
  2. Центр заголовка
  3. Меню

Заголовок панели инструментов не центрируется при установке Меню.

Я устанавливаю видимость динамического меню c на основе ответа API.

Посмотрите ниже.

ToolBar без меню.

enter image description here

ToolBar с Menu элемент Видимый

enter image description here

enter image description here

Код панели инструментов

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/blue”
    app:elevation="0dp">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/blue”
        android:contentInsetStart="0dp"
        android:contentInsetLeft="0dp"
        android:contentInsetEnd="0dp"
        android:contentInsetRight="0dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/img_profile"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                android:src="@drawable/profile_icon"
                android:visibility="visible" />

                <TextView
                    android:id="@+id/toolbar_title"
                    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_gravity="center"
                    android:layout_centerInParent="true"
                    android:gravity="center"
                    android:text="mySet"
                    android:textColor="@color/white"
                    android:textSize="12dp"
                    android:visibility="visible />

        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>

Объявление меню в MainActivity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_right_icons, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}

Видимость меню Генделя в другом фрагменте с использованием приведенного ниже кода

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.action_live).setVisible(true);
    menu.findItem(R.id.action_finish).setVisible(false);
    super.onPrepareOptionsMenu(menu);
}

Как установить заголовок Toolbar в центре, когда отображается Menu?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...