android 如何在另一个图像视图上半重叠图像视图

how to half overlap imageview on another imageview in android

我怎样才能将来自布局 XML 文件的图像像这张图片一样相互重叠一半。

这可以通过 RelativeLayout 轻松实现,但您需要查看 CoordinatorLayout 及其功能。

您必须使用减号 (-) 设置边距顶部 values.It 将使您的图像视图与其他图像视图重叠

<RelativeLayout
                android:layout_width="@dimen/_90sdp"
                android:layout_height="@dimen/_90sdp">

                <ImageView
                    android:id="@+id/bg"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_90sdp"
                    android:background="@color/profile_bg"
                    android:scaleType="fitXY" />

                <ImageView
                    android:id="@+id/img_user"
                    android:layout_width="@dimen/_80sdp"
                    android:layout_height="@dimen/_80sdp"
                    android:layout_below="@+id/bg"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="@dimen/_minus50sdp"
                    android:src="@drawable/place_holder_profile"/>
    </RelativeLayout>

使用这个依赖编译'com.intuit.sdp:sdp-android:1.0.2' https://github.com/intuit/sdp

只需使用 RealtiveLayout 和负边距

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_landing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/screen_background">


    <ImageView
        android:background="@color/black"
        android:id="@+id/img_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_below="@+id/img_background"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-40dp"<!-- negative margin to half of height -->
        android:src="@mipmap/ic_launcher" />

</RelativeLayout>

FrameLayout 已针对执行此功能进行了更优化。我承担的开销更少。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_landing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >


    <ImageView
        android:id="@+id/img_background"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:src="@mipmap/ic_launcher"
        android:background="@color/colorPrimary"/>

    <ImageView
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_below="@+id/img_background"
        android:layout_gravity="bottom|center_horizontal"
        android:src="@mipmap/ic_launcher"
        android:background="@color/cardview_dark_background"/>

</FrameLayout>

试试这个,看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_image"
                android:layout_width="96dp"
                android:layout_height="96dp"
                android:src="@drawable/cc"
                app:civ_border_width="2dp"
                app:civ_border_color="#FF000000"
                android:layout_centerInParent="true"
                />

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

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:alpha="0.4"
                        android:src="@drawable/a123123t"/>

                </FrameLayout>

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <!--blank space-->

                </FrameLayout>


            </LinearLayout>

        </RelativeLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <FrameLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent">
                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/profile_image1"
                        android:layout_width="96dp"
                        android:layout_height="96dp"
                        android:src="@drawable/cc"
                        android:padding="8dp"
                        android:layout_gravity="center"
                        app:civ_border_width="2dp"
                        app:civ_border_color="#FF000000"
                        android:layout_centerInParent="true"
                        />

                </FrameLayout>

                <FrameLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent">
                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/profile_image2"
                        android:layout_width="96dp"
                        android:layout_height="96dp"
                        android:src="@drawable/cc"
                        android:padding="8dp"
                        android:layout_gravity="center"
                        app:civ_border_width="2dp"
                        app:civ_border_color="#FF000000"
                        android:layout_centerInParent="true"
                        />

                </FrameLayout>

                <FrameLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent">

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/profile_image3"
                        android:layout_width="96dp"
                        android:layout_height="96dp"
                        android:src="@drawable/cc"
                        android:padding="8dp"
                        android:layout_gravity="center"
                        app:civ_border_width="2dp"
                        app:civ_border_color="#FF000000"
                        android:layout_centerInParent="true"
                        />
                </FrameLayout>






            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="vertical">

                <android.support.v4.widget.Space
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Edit Profile"/>
            </LinearLayout>

        </LinearLayout>



    </LinearLayout>

</LinearLayout>

对于依赖项:

compile 'de.hdodenhof:circleimageview:2.1.0'

试试这个

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/layout_background">
        <ImageView
            android:id="@+id/img_background"
            android:layout_width="match_parent"
            android:layout_height="108dp"
            android:background="@color/up"
            android:src="@mipmap/ic_launcher" />

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/nav_header_view_profilePic"
            android:layout_width="108dp"
            android:layout_height="108dp"
            android:layout_below="@+id/img_background"
            android:src="@drawable/profile"
            android:layout_marginTop="-54dp"
            android:layout_centerHorizontal="true"
            />
    </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_mainProfile"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gradientLeft"
    android:orientation="vertical"
    tools:context=".Activity.ProfileActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_profileActivity"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/side_nav_bar"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:titleTextColor="@color/white" />

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


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <ImageView
                android:id="@+id/header_cover_image"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:scaleType="centerCrop"
                android:src="@drawable/background" />

            <ImageView
                android:id="@+id/img_profile"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_below="@+id/header_cover_image"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="-130dp"
                android:elevation="5dp"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_profile" />

            <RelativeLayout
                android:id="@+id/profile_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/header_cover_image"
                android:elevation="4dp"
                android:paddingBottom="24dp">


                <ImageView
                    android:id="@+id/img_editProfile"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="30dp"
                    android:clickable="true"
                    android:src="@android:drawable/ic_menu_edit"
                    android:visibility="gone" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/img_editProfile"
                    android:gravity="center_horizontal"
                    android:orientation="vertical"
                    android:layout_marginTop="80dp"
                    android:paddingStart="20dp"
                    android:paddingEnd="20dp">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:layout_marginBottom="10dp">

                        <EditText
                            android:id="@+id/et_fnameProfile"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:backgroundTint="@color/black_overlay"
                            android:editable="true"
                            android:hint="@string/hint_first_name"
                            android:inputType="textPersonName"
                            android:paddingStart="10dp"
                            android:paddingEnd="10dp"
                            android:textColor="@color/white"
                            android:textColorHint="@color/white"
                            android:textSize="18sp" />

                        <EditText
                            android:id="@+id/et_lnameProfile"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:backgroundTint="@color/black_overlay"
                            android:editable="true"
                            android:hint="@string/hint_last_name"
                            android:inputType="textPersonName"
                            android:paddingStart="10dp"
                            android:paddingEnd="10dp"
                            android:textColor="@color/white"
                            android:textColorHint="@color/white"
                            android:textSize="18sp" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:layout_marginBottom="10dp">
                    <EditText
                        android:id="@+id/et_countryCodeProfile"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:backgroundTint="@color/black_overlay"
                        android:editable="true"
                        android:hint="@string/hint_country_code"
                        android:inputType="number"
                        android:paddingStart="10dp"
                        android:paddingEnd="10dp"
                        android:textColor="@color/white"
                        android:textColorHint="@color/white"
                        android:textSize="18sp" />


                    <EditText
                        android:id="@+id/et_mobileNoProfile"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:backgroundTint="@color/black_overlay"
                        android:editable="true"
                        android:hint="@string/hint_mobile_no"
                        android:inputType="number"
                        android:paddingStart="10dp"
                        android:paddingEnd="10dp"
                        android:textColor="@color/white"
                        android:textColorHint="@color/white"
                        android:textSize="18sp" />
                    </LinearLayout>
                </LinearLayout>

                <ProgressBar
                    android:id="@+id/progress_profile"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true" />
            </RelativeLayout>

        </RelativeLayout>

    </ScrollView>

</LinearLayout>

在图像视图中使用此行

android:海拔高度="@dimen/dp5"