Android ツールバーの中央のタイトルとカスタムフォント 質問する

Android ツールバーの中央のタイトルとカスタムフォント 質問する

ツールバーのタイトルにカスタム フォントを使用し、それをツールバーの中央に配置する正しい方法 (クライアントの要件) を見つけようとしています。

現時点では、古き良き ActionBar を使用しており、タイトルを空の値に設定し、setCustomViewカスタム フォントの TextView を配置して、ActionBar.LayoutParams を使用して中央に配置しています。

もっと良い方法はありますか? 新しいツールバーを ActionBar として使用します。

ベストアンサー1

カスタム タイトルを使用するには、Toolbarこれが単なる派手な ViewGroup であることを覚えておくToolbarだけで、次のようにカスタム タイトルを追加できます。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?android:attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toolbar Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />


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

TextViewこれは、通常の なので、好きなようにスタイル設定できることを意味しますTextView。アクティビティでは、次のようにタイトルにアクセスできます。

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);

おすすめ記事