折叠式工具栏中的工具栏,标题未显示

15

我将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>

我想使用以下代码设置Toolbar的标题,但是它没有生效。标题就是没有显示出来。

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

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

我也尝试过使用以下代码在CollapsingToolbarLayout中设置标题,但它也没有起作用。

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

但是,如果我从布局中删除了CollapsingToolbarLayout,并将AppBarLayout作为Toolbar的直接父项,则上面用于设置Toolbar标题的代码会起作用。

我错过了什么吗?这个问题很奇怪。这是设计支持库的一个bug吗?我如何在不改变布局的情况下解决它?


在你的CoordinatorLayoutAppBarLayoutCollapsingToolbarLayout上尝试使用android:fitsSystemWindows="true" - Farbod Salamat-Zadeh
@FarbodSalamat-Zadeh 谢谢您的回复。我按照您说的方法尝试了,但还是没有起作用。 - alijandro
2
有关这个问题有任何更新吗?我也遇到了同样的问题!! - Shatazone
4个回答

5

这是一个临时解决方案。我没有深入调查代码,但通过禁用CollapsingToolbarLayoutToolbar的刷新,它可以工作。
这就是我所做的:

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

1
你找到了这个的永久解决方案吗?我目前正在使用它,但我不知道它是否可靠。 - Bhargav
不确定为什么,但这个可以工作,目前来看还不错,我真的找不到其他做法。 - Roberto
这应该可以工作!非常好。实际上已经有错误报告了:https://code.google.com/p/android/issues/detail?can=2&q=183333&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened&id=183333我发现这个答案与这些帖子上的答案相关: https://dev59.com/pVwY5IYBdhLWcg3w5rWq | https://dev59.com/510a5IYBdhLWcg3wH1ly - Neon Warge

3

只需在XML中设置Collapsing toolbar的"titleEnabled=false",然后工具栏标题将会显示出来。

    app:titleEnabled="false"

0

试一下这个:

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

-1

移动

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

从onCreate到if内的onCreateView


为什么你们俩有同样的名字? - user3600801

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接