Android appcompat清单中的工具栏标题

时间:2014-11-12 04:36:59

标签: android android-appcompat android-toolbar

我在FragmentActivity中的抽屉布局中使用Appcompat工具栏。在每个fragmentActivity中,需要根据活动更改标题。有没有办法从Manifest设置标题,而不是在创建工具栏上设置每个活动.setTitle(“ABC”)。

注意:我没有使用getSupportActionBar(工具栏)

FirstActivity扩展了BaseActivity

的themes.xml     

<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDarker</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

<color name="primary">#457C50</color>
<color name="primaryDarker">#580C0C</color>

Android Manifest

<activity
    android:name="com.sample.md.FirstActivity"
    android:configChanges="orientation|screenSize|keyboardHidden"
    android:label="First Activity" >            
</activity>

工具栏在基本活动中初始化

android.support.v7.widget.Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
android.support.v7.app.ActionBarDrawerToggle mDrawerToggle = 
    new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            try {
                toolbar.setTitle(mTitle);
            } catch (Exception e) {
                e.printStackTrace();
            }
            invalidateOptionsMenu();
            syncState();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            toolbar.setTitle("Menu");
            invalidateOptionsMenu();
            syncState();
        }

};

mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();

3 个答案:

答案 0 :(得分:1)

您应该能够设置label属性。

答案 1 :(得分:1)

设置如下标签属性:

<activity
        android:label="My Screen"
        ... >

答案 2 :(得分:0)

在活动/片段中使用setTitle()