2016-08-15 150 views
2

我有一个CollapsingToolbar的问题。问题在于,默认情况下,当Activity刚创建时,我需要折叠工具栏。如果我通过appBarLayout.setExpanded(false, false)onCreate()方法通过代码执行此操作,我会首次看到扩展工具栏,然后在一秒钟内崩溃。我需要删除它并立即显示折叠的工具栏。折叠工具栏默认折叠

UPD:

<android.support.design.widget.AppBarLayout 
    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:id="@+id/app_bar_layout" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:contentScrim="@color/colorPrimary" 
     app:expandedTitleMarginEnd="64dp" 
     app:expandedTitleMarginStart="16dp" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

    <ImageView 
     android:id="@+id/toolbar_image_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:fitsSystemWindows="true" 
     tools:ignore="ContentDescription"/> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?actionBarSize" 
     android:background="@android:color/transparent" 
     app:layout_collapseMode="pin"> 
    </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 

回答

7

AppBarLayout具有expanded属性。尝试将其设置为false。

<android.support.design.widget.AppBarLayout 
    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:id="@+id/app_bar_layout" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:fitsSystemWindows="true" 
    app:expanded="false"> 

这在我的测试项目中工作时,我插入了您的布局并添加了一行。

我能够在设计支持库的版本23.0.1和23.4.0中做到这一点。

+1

这有没有结果( – Near1999

+0

会发生什么,是折叠式工具还是完全展开? –

+0

这工作在我的测试项目,请用后'CoordinatorLayout'您的整个布局XML在里面。 –