【问题标题】:CollapsingToolbarLayout title is gone when expandedCollapsingToolbarLayout 展开时标题消失
【发布时间】:2015-09-24 13:04:44
【问题描述】:

我有这种 xml 的平静。

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            app:toolbarId="@+id/toolbar"
            android:id="@+id/collapsing"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize" />
 </android.support.design.widget.CollapsingToolbarLayout>

它根本没有显示标题。 (下图)

但是当 CollapsingToolbarLayout 是有点折叠的标题显示。

我在创建方法中设置了活动标题。

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

        CollapsingToolbarLayout layout = (CollapsingToolbarLayout) findViewById(R.id.collapsing);
        layout.setTitle(toolbar.getTitle());
        layout.invalidate();

完整的 xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


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


        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:paddingBottom="?attr/actionBarSize"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_height="match_parent"/>


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



            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:minHeight="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                app:toolbarId="@+id/toolbar"
                android:id="@+id/collapsing"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways">


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize" />


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

            <LinearLayout
                android:id="@+id/custom_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="invisible"/>


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


    <LinearLayout
        android:id="@+id/drawer_linear"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:background="@color/drawer_grey"
            android:divider="@drawable/drawer_divider"
            android:scrollbars="none"
            android:dividerHeight="1px" />

    </LinearLayout>

有什么方法可以让标题始终显示吗?

【问题讨论】:

    标签: android android-collapsingtoolbarlayout


    【解决方案1】:

    我遇到了同样的问题,经过数小时的搜索,我找到了解决方案,并希望与您分享。

    1. 使用 com.android.support:design:23.1.0
    2. 你的布局应该是这样的:

      <android.support.design.widget.CoordinatorLayout
          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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      
                 <android.support.v7.widget.Toolbar
                          android:id="@+id/toolbar"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          app:layout_scrollFlags="scroll|enterAlways|snap />
      

    我希望我能帮助别人。

    解决办法来自this的帖子。

    【讨论】:

    • 这个答案被否决了,但它可能是正确的答案,因为AppBarLayout 期望成为CoordinatorLayout 的第一个孩子。 OP 一定有问题,因为他/她将FrameLayout 作为第一个孩子。
    • @AliKazi 是的,可能是这样。
    【解决方案2】:

    您只需在 v-21 styles.xml 中将其添加到您的主题中。

    <item name="android:windowTranslucentStatus">true</item>
    

    【讨论】:

    • 不能解决问题。我需要完全扩展的工具栏有标题文本。我不需要透明状态栏(还没有)。
    • 好吧,也许它可以工作,有点。但仍然不是我正在寻找的东西。自从为〜4.0及更高版本的android制作应用程序以来。
    • 它是 Lollipop 的解决方法,因为您已经在使用 AppCompat,所以在 pre lollipop 设备上不会有问题。
    • @Alpha 从您的 CollapsingToolbarLayout 中删除此 android:fitsSystemWindows="true"。它会正常工作的。
    • 在 4.3 android 上根本不工作。在 5.0.0 上,工具栏低于状态栏并扭曲。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多