支持工具栏标题

时间:2015-04-08 20:31:23

标签: android android-toolbar

无论我做什么,我都无法在支持工具栏中设置标题。无论我是否设置它,工具栏上唯一的东西就是一个图标。

遵循以下指南: http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html

我已经尝试了getSupportActionBar.set的每个组合......而且根本没有改变工具栏。

的活动:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.gallery);
    mToolbar = (Toolbar) findViewById(R.id.galleryToolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setSubtitle("blah blah");
    getSupportActionBar().setTitle("blah");
    getSupportActionBar().setLogo(R.drawable.icon);
    ...
}

布局:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/galleryLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:id="@+id/galleryToolbar"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

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

主题:

<style name="GalleryTheme" parent="@style/Theme.AppCompat.NoActionBar">
    <item name="windowActionModeOverlay">true</item>
    <item name="android:windowBackground">@android:color/black</item>
    <item name="showcaseViewStyle">@style/CustomShowcaseTheme</item>
    <item name="actionOverflowButtonStyle">@style/Custom.Widget.ActionButton.Overflow</item>
</style>

1 个答案:

答案 0 :(得分:1)

如果您不使用setSupportActionBar(mToolbar)将工具栏设置为操作栏,则下面的代码段将允许您更改支持工具栏上的标题。

使用 mToolbar 实例调用 setTitle()方法。

 mToolbar = (Toolbar) findViewById(R.id.galleryToolbar);

    if (mToolbar != null) {
        mToolbar.setTitle("My Title");
    }

此外,请在 mToolbar 实例上调用其他方法以进一步自定义。

有关详细信息,请查看developer docs

相关问题