在某些设备上,CollapsingToolbarLayout 中的 ImageView 不可见

7

我正在使用CollapsingToolbarLayout,并在其中放置了两个图像,一个用作背景,一个用作上部徽标。想法是使它们都具有视差效果。这在Android 5的物理设备上运行良好,但在低版本的设备(或模拟器)上无法正常工作。有点奇怪。

这是我的布局:

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

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

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#fff">

            <!--content--> 
        </FrameLayout>

    </NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#222">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll">
            <ImageView
                android:id="@+id/header_bk"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/background"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7"
                />
            <ImageView
                android:id="@+id/header_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/ic_logo"
                app:layout_collapseMode="parallax"
                android:background="#44ff0000"
                />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
1个回答

9
我找到了一种方法使它能够工作。在低于5.0的Android版本中,CollapsingToolbarLayout中使用wrap_content的视图似乎存在问题。将其更改为match_parent并使用scale_type="center"使图像保持居中可以解决我的问题。
以下是图像布局的更改:
<ImageView
    android:id="@+id/header_logo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="center"
    android:layout_gravity="center"
    android:src="@drawable/ic_adi_logo"
    app:layout_collapseMode="parallax"
    />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接