当试图将它锚定在协调器布局中时ImageView是一半隐藏

问题描述:

当我试图将ImageView锚定在AppbarLayout中时,我遇到了一个奇怪的问题。当试图将它锚定在协调器布局中时ImageView是一半隐藏

在设计它看起来像这样:

enter image description here

,当我运行的应用程序,它看起来像:

enter image description here

我的代码:

<?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/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:descendantFocusability="beforeDescendants" 
    android:fitsSystemWindows="true" 
    android:focusableInTouchMode="true"> 

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

     <android.support.design.widget.CollapsingToolbarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:clipChildren="true" 
      app:contentScrim="@color/transparent" 
      app:layout_scrollFlags="enterAlways"> 

      <ImageView 
       android:id="@+id/banner_image" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:adjustViewBounds="true" 
       android:fitsSystemWindows="true" 
       android:scaleType="fitXY" 
       app:layout_collapseMode="parallax" 
       app:srcCompat="@color/blue"/> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin"/> 

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

    <RelativeLayout 
     android:id="@+id/signup_screen_form" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <TextView 
      android:id="@+id/screen_label" 
      android:text="@string/signup" 
      android:gravity="center" 
      android:textAllCaps="true" 
      android:padding="@dimen/screen_label_padding" 
      android:layout_alignParentTop="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

     <com.mypackage.android.views.register.StepIndicatorView 
      android:id="@+id/step_indicator" 
      android:layout_below="@id/screen_label" 
      android:layout_centerHorizontal="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/steps_recycler" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/layout_bottom" 
      android:layout_below="@id/step_indicator"/> 

     <LinearLayout 
      android:id="@id/layout_bottom" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:orientation="horizontal" 
      android:padding="@dimen/bottom_layout_padding"> 

      <Button 
       android:id="@+id/previous_button" 
       style="?borderlessButtonStyle" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/registration_steps_back" 
       android:textColor="@color/next_button_color" 
       android:textSize="@dimen/back_button_text_size"/> 

      <Button 
       android:id="@+id/next_button" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@drawable/sel_wizard_next_button" 
       android:foreground="?attr/selectableItemBackground" 
       android:text="@string/next" 
       android:textColor="@color/white" 
       android:textSize="@dimen/next_button_text_size"/> 
     </LinearLayout> 
    </RelativeLayout> 

    <android.support.v7.widget.CardView 
     app:layout_anchor="@+id/appbar" 
     app:layout_anchorGravity="bottom|center" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:src="@color/black" 
      android:layout_width="80dp" 
      android:layout_height="80dp"/> 
    </android.support.v7.widget.CardView> 
</android.support.design.widget.CoordinatorLayout> 

任何想法为什么发生这种情况?

+0

我想问题是你在'CollapsingToolbarLayout'上设置了'android:clipChildren =“true”'。你为什么这么做?如果不需要尝试删除它,它应该已经尝试 – rom4ek

+0

。它不是那个 – matrix

设置CardView标高使其位于AppBarLayout的前面。下面的XML应该足够了:

app:cardElevation="4dp" 

4dp应该工作,但它可能需要高一点。

我希望这对你有用。

+0

是的,实际上6dp的海拔高度工作,但是当它的API 19或更低版本时会发生什么? – matrix

+0

@matrix支持库应该照顾它。你知道它不适用于API 19及以下版本吗? – Cheticamp

+0

@matrix如果这是被接受的答案,不要忘记奖励赏金。 – Gary99