2016-10-08 60 views
0

我的工具欄中的標題未顯示。爲什麼不能在我的工具欄顯示中顯示標題?

這裏是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
tools:context="com.mddri_.myeats.main._MainActivity"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar_main" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:background="@color/colorPrimary" 
    android:elevation="6dp" 
    android:minHeight="?attr/actionBarSize" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:title="@string/app_label" 
    android:titleTextColor="@color/textColor" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabMode="scrollable" 
    android:layout_below="@+id/toolbar_main" 
    android:background="@color/colorPrimary" 
    /> 
<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/tab_layout"/> 

我不引用工具欄在我的Java代碼,但我可以告訴你,如果你需要它。我似乎應該能夠通過使用android:title="desired_title"來設置工具欄的標題。工具欄顯示,它是正確的顏色和大小,但它不顯示標題。

+0

嘗試getSupportActionBar()。setTitle(title); – darwin

回答

1

請使用以下內容。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main); 
setSupportActionBar(toolbar); 
getSupportActionBar().setTitle("Title"); 
+0

這段代碼在'setSupportActionBar(topToolBar)給了我一個錯誤; java.lang.RuntimeException:無法啓動活動ComponentInfo {com.mddri_.myeats/com.mddri_.myeats.main._Main Activity}:java.lang.IllegalStateException:此活動已經有一個窗口提供的操作欄裝飾。請勿在您的主題中請求Window.FEATURE_SUPPORT_ACTION_BAR並將windowActionBar設置爲false以代替使用工具欄。「 –

+0

正如錯誤解釋,您在嘗試設置工具欄之前沒有將默認操作欄設置爲false。使用'windowActionBar'設置爲false的樣式或者使用NoActionBar繼承父項的樣式(例如'Theme.AppCompat.Light.NoActionBar')聲明該活動的主題。看看這個錯誤,它有很多答案。 :] –