尝试将 ImageView 锚定在 Coordinatorlayout 中时,ImageView 半隐藏

ImageView is half hidden when try to anchor it in Coordinatorlayout

当我尝试将 ImageView 锚定在 AppbarLayout 中时,我遇到了一个奇怪的问题。

在设计器中看起来像这样:

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

我的代码:

<?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>

知道为什么会这样吗?

设置 CardView 高度,使其位于 AppBarLayout 之前。以下 XML 应该足够了:

   app:cardElevation="4dp"

4dp应该可以,但可能需要高一点。

希望这对你有用。