2017-03-04 67 views
0

我無法弄清楚爲什麼主要活動的操作欄中缺少應用程序名稱。我已經設置了標籤在清單如下:應用程序名稱在ActionBar中的MainActivity中缺失

<activity 
    android:name=".MainActivity" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

style.xml

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:windowContentOverlay">@null</item> 
    </style> 



</resources> 

我什至增加這主要活動:

public class MainActivity extends AppCompatActivity { 
... 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

} 

,但仍是應用名稱未在活動中顯示。除主要活動外,所有其他活動均顯示其標籤。有任何想法嗎?

+0

愚蠢的問題,但只是爲了驗證 - 它是在你的string.xml正確? – BR89

+0

@ BR89是的,我用硬編碼的字符串取代了它,但仍然沒有顯示它:( – user7659501

+0

嗯..當你第一次建立時,你的初始活動類型是什麼?Layout.xml是你主要活動的唯一文件嗎? – BR89

回答

0

試試下面的代碼:在的onCreate)你的活動

getSupportActionBar().setDisplayShowTitleEnabled(true); 
getSupportActionBar().setTitle(getResources().getString(R.string.app_name)); 
+0

20秒後發佈我的答案有人投票它,我沒有計算出用戶運行這個代碼的速度有多快,向該用戶致敬 –

+0

我沒有足夠的代表點投票,但我嘗試了這個,仍然沒有標題應用程序欄,它可以是樣式文件嗎? – user7659501

+0

我知道你沒有投票,嘗試使用'工具欄' –

0

設置標題(:

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

更改此:

android:theme="@style/SplashTheme" 

要:

android:theme="@style/AppTheme" 
清單中的活動標記中的

+0

我正在使用'getSupportActionBar', – user7659501

+0

那麼,我猜你正在使用工具欄。試試我編輯的答案。 – tahsinRupam

+0

我沒有我的佈局中的工具欄 – user7659501

0

試試這個在日清單:

機器人:主題= 「@風格/ AppTheme.NoActionBar」

看看是否顯示標題。如果你不使用工具欄,你不需要一個操作欄

相關問題