支持工具栏标题



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

遵循以下指南: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>

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

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

 mToolbar = (Toolbar) findViewById(R.id.galleryToolbar);
    if (mToolbar != null) {
        mToolbar.setTitle("My Title");
    }

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

有关更多信息,请查看开发人员文档

最新更新