工具栏字幕未显示

时间:2015-09-25 15:18:59

标签: android android-toolbar

我在一个折叠工具栏布局中设置我的工具栏,如下所示:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_collapseMode="pin"/>


    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

但是,当我尝试设置标题和副标题时,工具栏中只显示标题!

private void setupToolbar(){
    toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
    if(toolbar != null){
        setSupportActionBar(toolbar);
    }

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(mTitle);
    getSupportActionBar().setSubtitle("Subtitle);
}

如何访问工具栏的副标题?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您可以在布局xml中执行此操作:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:subtitle="ANDROID">
    <!-- sub title for the Toolbar-->
</android.support.v7.widget.Toolbar>

或者您可以通过编程方式执行:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); // get the reference of Toolbar
setSupportActionBar(toolbar); // Setting/replace toolbar as the ActionBar4
toolbar.setSubtitle("ANDROID"); // setting a sub title for this Toolbar
getSupportActionBar().setDisplayShowTitleEnabled(false); // hide the current title from the Toolbar