2017-09-24 168 views
0

我想在android中制作可折叠工具栏。我希望图像在用户滚动时折叠。Android可折叠工具栏不工作

XML:

<?xml version="1.0" encoding="UTF-8"?> 

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/lib/com.mridulahuja.kudamm" xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.mridulahuja.kudamm.activities.ProductInfoActivity"> 
    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     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="?attr/colorPrimary" 
      app:expandedTitleMarginEnd="64dp" 
      app:expandedTitleMarginStart="48dp" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 
      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:layout_collapseMode="pin" 
       android:minHeight="20dp" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
      <ImageView 
       android:id="@+id/imageView" 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:scaleType="centerCrop" 
       android:fitsSystemWindows="true" 
       app:layout_collapseMode="parallax" 
       android:src="@drawable/splash_background" 
       app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" 
       android:minHeight="100dp" /> 
     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" /> 
    </ScrollView> 
</android.support.design.widget.CoordinatorLayout> 

JAVA:

CollapsingToolbarLayout collapsingToolbar = 
      (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); 
collapsingToolbar.setTitle("Title"); 

这说明我这个在Android工作室:

enter image description here

但是当我运行它,它只是表明我这个:

enter image description here

我甚至试图用android.support.v4.widget.NestedScrollView而不是ScrollView,但它也没有工作。

任何想法我在做什么错???

回答

0

将NestedScrollView的高度更改为Wrap_content而不是match_parent,它占用整个屏幕。希望这对你有用。

+0

欢迎来到StackOverflow并感谢您的帮助。你可能想通过添加一些解释来让你的答案更好。 –