2017-10-04 3 views
1

ツールバーのタイトルにカスタムテキストが必要です。私は戻るボタン付きのツールバーでカスタムタイトルを使用する

<android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:titleTextColor="?attr/TextColor"> 
     <TextView 
      android:textColor="?attr/TextColor" 
      android:id="@+id/toolbar_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" 
      android:layout_gravity="center" /> 

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

以下のようにそれを追加し、Javaで私が

toolbar = (Toolbar) findViewById(R.id.toolbar); 
     Typeface font=Typeface.createFromAsset(AuthorQuoteActivityTabs.this.getAssets(), constant.font); 
     TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); 
     mTitle.setText("authorName"); 
     mTitle.setTypeface(font,Typeface.BOLD); 
     setSupportActionBar(toolbar); 
     if (getSupportActionBar() != null) { 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     } 

そのが正常に動作しますが、私は、ツールバーにカスタムタイトルテキストだけでなく、アプリ名を取得しています以下のようなコードを書かれています。私はそれに私のカスタムテキストだけが欲しい。デフォルトのタイトルを削除してツールバーのカスタムテキストのみを使用するにはどうすればよいですか?あなたは

getSupportActionBar().setTitle(""); 
+0

ツールバーに中央揃えのテキストが必要な場合は、 'Toolbar'内に別の' TextView'を置くだけでは不十分かもしれません。テキストの位置は、ツールバーの他の項目(戻るボタン、メニュー項目)によって異なります。 この質問を見ることができます:https://stackoverflow.com/questions/29443604/android-how-to-center-title-in-toolbar これは、より堅牢な中心のツールバーの実装を含んでいます – user2137020

答えて

2

このコードを使用してください:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    toolbar.setTitle("Toolbar Title"); 
    setSupportActionBar(toolbar); 

がそれを願っています助けて、

0

によって「通常」のタイトルを削除する必要があり、別のTextViewにタイトルを設定しているよう

おかげで

関連する問題