Подтвердить что ты не робот

Как центрировать заголовок на панели инструментов (даже при отображении кнопки панели инструментов)

Как центрировать заголовок действия на панели инструментов таким образом, чтобы он работал также с отображаемой кнопкой панели инструментов?

В настоящее время лучшим решением, которое я нашел, является наличие поля 60dp, если отображается обратная кнопка.

4b9b3361

Ответ 1

Я не знаю, как наилучшая практика, но вы можете попробовать это обходное решение, используя пользовательскую кнопку возврата на панели инструментов. Код макета:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp">

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

        <ImageView
            android:id="@+id/toolbar_back_button"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:src="@drawable/ic_back" />

        <TextView
            android:id="@+id/toolbar_title"
            style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" />

    </RelativeLayout>

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

и код в действии:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    TextView toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
    setSupportActionBar(toolbar);

    ImageView backButton = (ImageView) findViewById(R.id.toolbar_back_button);
    backButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           finish();
        }
    });

Ответ 2

При использовании ТОЛЬКО кнопки "Назад" это сработало для меня. В xml:

<android.support.v7.widget.Toolbar
    android:id="@+id/tb_title_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textAllCaps="true"
        android:textColor="#2B2B2B"
        android:textSize="14sp" />

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

В java-коде:

int contentInsetStartWithNavigation = mTitleContainer.getContentInsetStartWithNavigation();
mTitleContainer.setContentInsetsRelative(0, contentInsetStartWithNavigation);