1

我有一个Android支持设计折叠工具栏上的布局,其中包含TextView,但当我折叠我的工具栏。一些带有工具栏标题的TextView。 我想隐藏其他所有东西,而不是工具栏和标题。 这是我的布局。Android折叠工具栏没有隐藏其他元素,而折叠

<android.support.design.widget.AppBarLayout 
    android:id="@+id/MyAppbar" 
    android:layout_width="match_parent" 
    android:layout_height="256sp" 
    android:fitsSystemWindows="true"> 
    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapse_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     android:background="#ff009688" 
     app:contentScrim="@color/colorPrimary" 
     app:expandedTitleMarginTop="100sp" 
     app:expandedTitleGravity="center|bottom" 
     android:fitsSystemWindows="true"> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitXY" 
      app:srcCompat="@drawable/bgimg" 
      app:layout_collapseMode="parallax"/> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/MyToolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" /> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      app:layout_anchor="@+id/MyAppbar" 
      android:layout_height="match_parent"> 
      <de.hdodenhof.circleimageview.CircleImageView 
       android:id="@+id/profile_image" 
       android:layout_width="96dp" 
       android:layout_height="96dp" 
       android:src="@drawable/studentprofile" 
       app:civ_border_width="2dp" 
       app:civ_border_color="#0adcf4" 
       android:layout_marginTop="58dp" 
       android:layout_alignParentTop="true" 
       android:layout_centerHorizontal="true" /> 

      <TextView 
       android:id="@+id/branch" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="Branch: " 
       android:textColor="@color/textColorPrimary" 
       android:textSize="14sp" 
       android:layout_marginBottom="20sp" 
       android:layout_alignParentBottom="true" 
       android:layout_centerInParent="true" /> 
     </RelativeLayout> 
    </android.support.design.widget.CollapsingToolbarLayout> 

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

我的Java代码...

collapsingToolbar = 
      (CollapsingToolbarLayout) findViewById(R.id.collapse_toolbar); 
    collapsingToolbar.setTitle(name); 
    collapsingToolbar.setExpandedTitleTextAppearance(R.style.expandedappbar); 
    collapsingToolbar.setCollapsedTitleTextAppearance(R.style.collapsedappbar); 
    final ImageView profile=(ImageView)findViewById(R.id.profile_image); 
    Picasso.with(this) 
      .load(photo_url) 
      .placeholder(R.drawable.studentprofile) 
      .networkPolicy(NetworkPolicy.NO_CACHE) 
      .into(new Target() { 
       @Override 
       public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){ 
       /* Save the bitmap or do something with it here */ 
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { 

         @Override 
         public void onGenerated(Palette palette) { 
          collapsingToolbar.setContentScrimColor(palette.getMutedColor(ContextCompat.getColor(context,R.color.colorPrimary))); 
          collapsingToolbar.setStatusBarScrimColor(palette.getMutedColor(ContextCompat.getColor(context,R.color.colorPrimaryDark))); 
         } 
        }); 
        //Set it in the ImageView 
        profile.setImageBitmap(bitmap); 
       } 

       @Override 
       public void onBitmapFailed(Drawable errorDrawable) { 
       } 

       @Override 
       public void onPrepareLoad(Drawable placeHolderDrawable) { 
       } 
      }); 

回答

2

自从我解决我的问题,我自己我张贴这个答案,因此将有益他人。 CollapsingToolBar延伸FrameLayout所以任何写在另一个标签后面的xml标签都会被分层为父项的顶部。我的RelativeLayout是在Toolbar之后写的那就是为什么Relative Layout元素在CollapsingToolbar崩溃后在Toolbar上显示。 只是在Toolbar之前将RelativeLayout隐藏在工具栏折叠后的元素上。

<android.support.design.widget.CollapsingToolbarLayout 
    android:id="@+id/collapse_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_scrollFlags="scroll|exitUntilCollapsed" 
    android:background="#ff009688" 
    app:contentScrim="@color/colorPrimary" 
    app:expandedTitleMarginTop="100sp" 
    app:expandedTitleGravity="center|bottom" 
    android:fitsSystemWindows="true"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" 
     app:srcCompat="@drawable/bgimg" 
     app:layout_collapseMode="parallax"/> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     app:layout_anchor="@+id/MyAppbar" 
     android:layout_height="match_parent"> 
     <de.hdodenhof.circleimageview.CircleImageView 
      android:id="@+id/profile_image" 
      android:layout_width="96dp" 
      android:layout_height="96dp" 
      android:src="@drawable/studentprofile" 
      app:civ_border_width="2dp" 
      app:civ_border_color="#0adcf4" 
      android:layout_marginTop="58dp" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" /> 

     <TextView 
      android:id="@+id/branch" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="Branch: " 
      android:textColor="@color/textColorPrimary" 
      android:textSize="14sp" 
      android:layout_marginBottom="20sp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerInParent="true" /> 
    </RelativeLayout> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/MyToolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     app:layout_collapseMode="pin" /> 

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

我发现这一点就像我在测试相同的解决方案:)。做得好! – Radu

0

试试这个:

改变你的CollapsingToolbarLayout布局属性:

使用:

app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" 

代替

app:layout_scrollFlags="scroll|exitUntilCollapsed" 

与这个属性添加到工具栏组件

android:minHeight="?attr/actionBarSize" 

说明:

假设enterAlways声明并且已指定一个minHeight,你也可以指定enterAlwaysCollapsed。使用此设置时,您的视图将仅出现在此最小高度处。只有当滚动到达顶端将视图扩展到完全高度:

的详细信息:https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

+0

做完这个工具栏后隐藏在折叠模式 –