戻るボタンが有効になっているとツールバーのタイトルが中央に表示されない 質問する

戻るボタンが有効になっているとツールバーのタイトルが中央に表示されない 質問する

ツールバーのタイトルを中央に表示しようとしていますが、そのためにはこの回答に示されている方法を使用します:-ツールバーセンターのタイトル

activityただし、次のコードで戻るボタンを有効にすると、

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);

ツールバーのタイトルが中央に表示されず、わずかに右にずれて表示されます。

中央揃えのタイトルを実現するにはどうすればいいですかそれなしいる戻るボタンやメニューバーの影響を受けますか?

ベストアンサー1

ツールバー内に TextView を追加し、TextView 内で次の属性を設定することを忘れないでください。

android:layout_marginRight="?android:attr/actionBarSize"

または

android:layout_marginEnd="?android:attr/actionBarSize"

コードスニペット:

<android.support.v7.widget.Toolbar
    android:id="@+id/custom_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@android:color/holo_red_dark">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="abc"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_marginRight="?android:attr/actionBarSize"
        android:gravity="center"/>

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

参照するこれ詳細についてはチュートリアルをご覧ください。

おすすめ記事