如何设置图像的一部分与应用栏重叠

时间:2016-12-21 02:55:17

标签: android android-toolbar android-collapsingtoolbarlayout

我将Toolbar包裹在CollapsingToolbarLayout内以使用图片创建折叠工具栏效果,以便工具栏使用容器布局响应滚动事件AppBarLayout

我想添加另一张图片,其顶部与应用栏重叠,就像播放商店应用中的海报电影图片一样,图片的下半部分与内容布局重叠

我的代码看起来像这样

<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:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed|snap">


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="220dp"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/scarlett_johansson"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/app_name"
            android:textSize="40sp" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

My app Looks like this

我想在Play商店应用

中添加另一个ImageView重叠应用栏,就像电影海报一样

i want to set  image overlap the app bar like movie poster like play store app

2 个答案:

答案 0 :(得分:2)

你必须这样做

<android.support.design.widget.CoordinatorLayout> 
  <android.support.design.widget.AppBarLayout>     
    <android.support.design.widget.CollapsingToolbarLayout>          
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.CollapsingToolbarLayout>
  </android.support.design.widget.AppBarLayout>

  <android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:behavior_overlapTop="52dp"
    app:expandedTitleMarginBottom="70dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
   </RelativeLayout>
  </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:-1)

为CollapsingToolbarLayout尝试以下代码:

<android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed|snap">




        <RelativeLayout
            android:layout_width="match_parent"
            android:background="#ffff"
            app:layout_collapseMode="parallax"
            android:layout_height="280dp">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="220dp"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/d"
                android:id="@+id/img"
                app:layout_collapseMode="parallax" />

            <View android:layout_height="90dp"
                android:layout_width="60dp"
                android:background="#dd7788"
                android:gravity="top"
                android:layout_alignBottom="@id/img"
                android:layout_marginBottom="-30dp"
                android:layout_marginLeft="10dp"/>

        </RelativeLayout>

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

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

您必须使用所需的ImageView替换View。 希望它会有所帮助。