2015-05-13 3 views
9

у меня есть панель инструментов в моем приложении так:Android - Центр название панели инструментов

<?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" /> 

И я добавить название, как:

getSupportActionBar().setTitle(title); 

Я читал, что Android toolbar center title and custom font

Но если я изменяю xml панели инструментов, я не знаю, как изменить значение TextView. Кто-то может мне помочь?

+0

использования, что другой ответ, и использовать findViewById для получить TextView – Knossos

+0

@Knossos Спасибо :) – benjyspider

ответ

26

Не используйте getSupportActionBar().setTitle(title);, чтобы установить заголовок, если у вас есть собственный макет панели инструментов.

Вместо предполагается, что ваш макет XML выглядит следующим образом:

 <!-- 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> 

Вам нужно позвонить, то в onCreate(), вероятно, ваш MainActivity «ы:

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

Спасибо :) @Karim – benjyspider

+1

Удивительное решение @Karim – lmiguelvargasf