如何在 Card 的外部和左侧添加一个 ImageView?

How to add an ImageView outside and to the left of a Card?

这就是我想要实现的目标

这就是我目前所拥有的,下面的代码是此屏幕截图的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="270dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_centerVertical="true" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/imageView"
                android:textColor="#000000"
                android:textSize="40sp" />
        </RelativeLayout>

    </android.support.v7.widget.CardView>

基本上,我无法弄清楚如何将图像放在外面但仍然触摸卡片。感谢任何帮助,谢谢。

试试这个布局

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

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
            android:id="@+id/first"
            app:cardBackgroundColor="@android:color/white"
            app:cardCornerRadius="@dimen/cardview_default_radius"
            app:cardElevation="4dp"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginLeft="50dp"
            card_view:cardUseCompatPadding="true">

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="32dp">

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="TEXT"
                    android:textSize="18sp"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</FrameLayout>

<ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="32dp"
        android:background="#000000"
        android:layout_marginTop="15dp"
        android:contentDescription="@null"/>

</RelativeLayout>

结果:

您可以使用 FrameLayoutelevation 来实现您的目标。

根据FrameLayout的文档

Child views are drawn in a stack, with the most recently added child on top

您需要调整 ImageView 的高度,使其高于 CardView

这就是我的成就:您可以根据设计修改您的paddings/margins。

代码:

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

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.CardView
        app:cardCornerRadius="4dp"
        app:cardBackgroundColor="@android:color/holo_blue_bright"
        android:layout_width="308dp"
        android:layout_height="112dp"
        android:layout_gravity="right"
        android:layout_margin="10dp">

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

            <TextView
                android:layout_marginStart="40dp"
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:paddingLeft="8dp"
                android:text="EARTH"
                android:textColor="#000000"
                android:textSize="70sp" />
        </LinearLayout>

    </android.support.v7.widget.CardView>

    <ImageView
        android:src="@drawable/planet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:elevation="2dp" />
</FrameLayout>
</LinearLayout>