简体   繁体   English

CollapsingToolbarLayout中的工具栏,工具栏标题未显示

[英]Toolbar inside CollapsingToolbarLayout, Toolbar title not showing

I used CollapsingToolbarLayout as the parent of Toolbar , below it the layout 我使用CollapsingToolbarLayout作为Toolbar的父级,在它下面是布局

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/test_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            app:navigationIcon="@drawable/abc_ic_ab_back_mtrl_am_alpha"
            app:theme="@style/ThemeOverlay.AppCompat.Light" />

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

Then I want to set the title of the Toolbar with the following code, but it didn't work. 然后我想用下面的代码设置Toolbar的标题,但它不起作用。 The title just didn't show. 标题只是没有显示。

    Toolbar toolbar = (Toolbar) findViewById(R.id.test_toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setTitle("ABC");

I also tried set the title in CollapsingToolbarLayout with the following code, it didn't work either. 我还尝试使用以下代码在CollapsingToolbarLayout设置标题,它也不起作用。

    CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
    collapsingToolbarLayout.setTitleEnabled(true);
    collapsingToolbarLayout.setTitle("ABC");

But if I removed CollapsingToolbarLayout from my layout and make AppBarLayout as the direct parent of Toolbar , the code above to set the title of Toolbar worked. 但是,如果我从布局中删除了CollapsingToolbarLayout并将AppBarLayout作为Toolbar的直接父级,则上面的代码设置Toolbar的标题。

Did I missed something? 我错过了什么吗? This issue is so weird. 这个问题太奇怪了。 Is it a bug in design support library? 这是设计支持库中的错误吗? How can I solve it without changing my layout? 如何在不改变布局的情况下解决问题?

This is a temporary solution. 这是一个临时解决方案。 I didn't investigate the code deeply, but by disabling the refresh of Toolbar in CollapsingToolbarLayout , it worked. 我没有深入调查代码,但通过在CollapsingToolbarLayout禁用Toolbar的刷新,它可以工作。
Here is what I did: 这是我做的:

public static void setRefreshToolbarEnable(CollapsingToolbarLayout collapsingToolbarLayout,
                                           boolean refreshToolbarEnable) {
    try {
        Field field = CollapsingToolbarLayout.class.getDeclaredField("mRefreshToolbar");
        field.setAccessible(true);
        field.setBoolean(collapsingToolbarLayout, refreshToolbarEnable);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

只需在xml中设置“折叠”工具栏“titleEnabled = false”,即可显示工具栏标题。

    app:titleEnabled="false"

Move 移动

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
collapsingToolbarLayout.setTitleEnabled(true);
collapsingToolbarLayout.setTitle("ABC");

from onCreate to onCreateView inside if 从onCreate到onCreateView里面if

try this : 试试这个 :

collapsingToolbarLayout.setTitleEnabled(false);
toolbar.setTitle("ABC");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM