【问题标题】:Toolbar subtitle not appearing工具栏副标题不出现
【发布时间】:2015-12-23 11:30:23
【问题描述】:

我在一个 collapsingtoolbarlayout 中设置了我的工具栏,如下所示:

<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);
}

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

【问题讨论】:

标签: android android-toolbar


【解决方案1】:

这是一个 CollapsingToolbarLayout 错误。

阅读http://saulmm.github.io/mastering-coordinator 并访问一些图书馆:

https://github.com/anton46/WhatsApp-ProfileCollapsingToolbar,

https://github.com/hendraanggrian/collapsingtoolbarlayout-subtitle,

https://github.com/ahmadmuzakki/subtitle-collapsingtoolbar

https://github.com/k0shk0sh/CoordinatorLayoutExample 或您喜欢的任何其他人。

您还可以阅读我关于 Android 4.4 中阴影的回答:https://stackoverflow.com/a/48526318/2914140

更新

目前我可以看到已删除的答案。也许它会帮助某人。从源代码复制粘贴 (@q126y)。

这是因为在调用 setSubtitle 时工具栏尚未呈现。

这样调用代码。

mToolbar.post(new Runnable() {
        @Override
        public void run() {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(mTitle);
            getSupportActionBar().setSubtitle("Subtitle");
        }
    });

【讨论】:

    【解决方案2】:

    您可以在布局 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
    

    【讨论】:

    • 这仅在您不使用 CollapsingToolbarLayout 时有效
    猜你喜欢
    • 2015-09-30
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多