2015-05-13 4 views
9

ich so eine Toolbar in meiner app haben:Android - Zentrum der Titel der Toolbar

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    android:background="#263355" 
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

Und ich fügen Sie den Titel wie:

getSupportActionBar().setTitle(title); 

Ich habe gelesen, dass Android toolbar center title and custom font

Aber wenn ich das XML der Symbolleiste ändern, weiß ich nicht, wie man den Wert der TextView ändert. Jemand kann mir helfen?

+0

verwenden, die andere Antwort, und verwenden Sie findViewById zu Holen Sie sich die TextView – Knossos

+0

@Knossos Danke :) – benjyspider

Antwort

26

Verwenden Sie nicht getSupportActionBar().setTitle(title);, um den Titel festzulegen, wenn Sie ein benutzerdefiniertes Toolbar-Layout haben.

Stattdessen sieht wie folgt aus XML-Layout vorausgesetzt:

 <!-- Toolbar --> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/main_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <LinearLayout 
       android:id="@+id/main_toolbar_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/main_toolbar_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

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

Sie müssen irgendwo nennen in Ihrem MainActivity ‚s onCreate() wahrscheinlich:

((TextView) findViewById(R.id.main_toolbar_title)).setText("Title!"); 
+1

Danke :) @Karim – benjyspider

+1

Ehrfürchtige Lösung @Karim – lmiguelvargasf