在RelativeLayout中将Android Cardview置于父Cardview顶部

6

如何在下面的屏幕截图中将CardView放在父CardView的顶部?

enter image description here

当我将图像放入CardView时,会导致我的视图损坏

正确的视图应该是:

enter image description here

我的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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:background="@drawable/content_background">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/activity_main_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="65dp"
                    android:background="@color/colorPrimary"
                    android:titleTextColor="#ffffff">

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

                        <com.joanzapata.iconify.widget.IconTextView
                            android:id="@+id/icon_signal_robot"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_marginRight="10dp"
                            android:clickable="true"
                            android:gravity="center|right"
                            android:shadowColor="#22000000"
                            android:shadowDx="3"
                            android:shadowDy="3"
                            android:shadowRadius="1"
                            android:text="{fa-android}"
                            android:textColor="@color/quote"
                            android:textSize="30sp"/>

                        <com.joanzapata.iconify.widget.IconTextView
                            android:id="@+id/icon_search_icon"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_marginRight="10dp"
                            android:clickable="true"
                            android:gravity="center|right"
                            android:shadowColor="#22000000"
                            android:shadowDx="3"
                            android:shadowDy="3"
                            android:shadowRadius="1"
                            android:text="{fa-search}"
                            android:textColor="#ffffff"
                            android:textSize="25sp"/>

                        <com.gigamole.library.ntb.NavigationTabBar
                            android:id="@+id/market_detail_navigation_tab_bar"
                            android:layout_width="90dp"
                            android:layout_height="30dp"
                            android:layout_gravity="center_vertical|left"
                            android:background="@drawable/bg_round_circle"
                            app:ntb_active_color="#4527A0"
                            app:ntb_animation_duration="150"
                            app:ntb_corners_radius="50dp"
                            app:ntb_inactive_color="#dddfec"
                            app:ntb_preview_colors="@array/red_wine"/>

                        <TextView
                            android:id="@+id/signal_application_detail_activity_title"
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:gravity="center|right"
                            android:text="@string/app_name"
                            android:textColor="#ffffff"
                            android:textSize="19sp"/>

                        <ImageView
                            android:id="@+id/activity_main_hearing_status"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="10dp"
                            android:gravity="center|right"
                            android:src="@drawable/ic_antenna_white"/>

                    </LinearLayout>
                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.AppBarLayout>

            <LinearLayout
                android:id="@+id/signal_loading_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="@color/colorPrimary"
                android:gravity="center"
                android:orientation="vertical"
                android:visibility="gone">

                <TextView
                    android:id="@+id/loading_view_title"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="@string/app_name"
                    android:textColor="#FFF"
                    android:textSize="35sp"
                    />

                <TextView
                    android:id="@+id/loading_view_description"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="@string/app_description"
                    android:textColor="#FFF"
                    android:textSize="18dp"
                    />

                <com.zl.reik.dilatingdotsprogressbar.DilatingDotsProgressBar
                    android:id="@+id/dots_progressBar"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:color="#ffee00"
                    android:radius="4dp"
                    app:dd_animationDuration="300"
                    app:dd_horizontalSpacing="7dp"
                    app:dd_numDots="5"
                    app:dd_scaleMultiplier="1.5"
                    />
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/frame"
                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:layout_gravity="center_vertical"
                android:background="@drawable/content_background">

                <android.support.v7.widget.CardView
                    android:id="@+id/malam"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/imageView2"
                        android:layout_width="match_parent"
                        android:layout_height="230dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/background"/>
                </android.support.v7.widget.CardView>


                <LinearLayout
                    android:id="@+id/linearLayout3"
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="120dp"
                    android:fitsSystemWindows="true"
                    android:orientation="vertical">

                    <android.support.v7.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentTop="true"
                        app:cardCornerRadius="0dp"
                        app:elevation="2dp">

                    </android.support.v7.widget.CardView>
                </LinearLayout>
                <ImageView
                    android:id="@+id/robot_dialog_icon"
                    android:layout_width="65dp"
                    android:layout_height="65dp"
                    android:layout_alignBottom="@+id/malam"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="75dp"
                    android:fitsSystemWindows="true"
                    android:src="@drawable/dialog_robot_dialog"
                    app:layout_collapseMode="parallax"/>
            </RelativeLayout>

        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>

</FrameLayout>
3个回答

3

框架布局和协调器布局的工作方式相同:第一个View是最底层的,下一个View位于前一个上面。

因此,我认为你应该只需更改在xml中的CardViews的顺序。

您还可以尝试使用android:elevation来实现所需的效果。

经过一些尝试,我已经获得了这样的结果:

我的xml是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/frame"
    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:layout_gravity="center_vertical"
    android:background="@color/colorAccent">

    <android.support.v7.widget.CardView
        android:id="@+id/malam"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="230dp"
            android:scaleType="centerCrop"
            android:background="@android:color/holo_blue_dark"/>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/white_card"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="120dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:cardCornerRadius="0dp"
        app:elevation="10dp">
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:translationY="32dp"
        app:layout_anchor="@id/white_card"
        app:layout_anchorGravity="center_horizontal"
        android:layout_gravity="center_horizontal">
    <ImageView
        android:id="@+id/robot_dialog_icon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"/>
    </android.support.v7.widget.CardView>

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

android:elevation 没有改变我的视图。 - DolDurma
你的xml中CardView的顺序如何? - Vladislav Sazanovich
我已经将XML布局粘贴到我的帖子上了,你能测试一下吗? - DolDurma

3

CardView默认的cardElevation值为4。因此,所有放在同一父布局下的其他视图都会在该CardView下面。

在您的代码中,id为“malam”的CardView将位于其他视图之上,因为它具有默认的cardElevation值。尝试将其cardElevation设置为0dp以覆盖默认值。

<android.support.v7.widget.CardView
        android:id="@+id/malam"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="0dp">

更新

将0dp设置为CardView的属性会移除该卡片的阴影。如果需要阴影,则可以采取以下两种方法:

  1. 从CardView "malam"中删除cardElevation属性,并将"android:elevation"设置为id为"linearLayout3"和"robot_dialog_icon"的视图,使其值大于4dp。
    注意:这仅适用于Lollipop及更高版本

  2. 将"linearLayout3"和"robot_dialog_icon"分别包裹在独立的CardViews中。


set to 0是因为移除了CardView的阴影。 - DolDurma
设置为 0 将会移除阴影。如果您需要它,可以将 android:elevation="5dp" 设置到其他视图 "linearLayout3" 和 "robot_dialog_icon" 中,并从 "malam" 中移除 cardElevation 属性。但是 elevation 属性仅在 Android 版本 => 21 中起作用。 - Sujay
另一个解决方法是将“linearLayout3”和“robot_dialog_icon”分别包装在个别的CardView中,这样它就在所有版本中起作用。 - Sujay

0

我不明白你想要什么,但我认为你想要以下的东西。

如果你想要这种类型的图片

enter image description here

然后使用这段代码。

XML 代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.softeng.aab.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.design.widget.CoordinatorLayout
        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">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp">

            <android.support.v7.widget.CardView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/card_view"
                card_view:cardCornerRadius="1dp"
                android:foreground="?android:attr/selectableItemBackground"
                android:layout_marginTop="5dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    >
                    <ProgressBar
                        android:id="@+id/progress"
                        android:visibility="visible"
                        style="?android:attr/progressBarStyleSmall"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        />
                </RelativeLayout>
                <LinearLayout
                    android:id="@+id/content_loading"
                    android:visibility="invisible"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:padding="5dp"
                    android:orientation="vertical">


                    <TextView
                        android:id="@+id/text_description"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/abc_text_size_medium_material"
                        android:layout_marginBottom="5dp" />

                    <TextView
                        android:id="@+id/text_owner"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/abc_text_size_small_material"
                        android:layout_marginBottom="5dp" />

                    <TextView
                        android:id="@+id/text_networks"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/abc_text_size_small_material"
                        />

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

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

        </RelativeLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            app:layout_anchor="@id/card_view"
            app:layout_anchorGravity="right|end|bottom"
            android:layout_marginRight="@dimen/fab_margin">

        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            >
            <ImageView
                android:id="@+id/image_avatar"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@android:color/holo_red_dark"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

更新:

enter image description here

我认为你需要这个

代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.softeng.aab.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.design.widget.CoordinatorLayout
        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">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            >

            <android.support.v7.widget.CardView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/card_view"
                card_view:cardCornerRadius="1dp"
                android:foreground="?android:attr/selectableItemBackground"
                android:layout_marginTop="5dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    >
                    <ProgressBar
                        android:id="@+id/progress"
                        android:visibility="visible"
                        style="?android:attr/progressBarStyleSmall"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        />
                </RelativeLayout>
                <LinearLayout
                    android:id="@+id/content_loading"
                    android:visibility="invisible"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:padding="5dp"
                    android:orientation="vertical">


                    <TextView
                        android:id="@+id/text_description"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/abc_text_size_medium_material"
                        android:layout_marginBottom="5dp" />

                    <TextView
                        android:id="@+id/text_owner"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/abc_text_size_small_material"
                        android:layout_marginBottom="5dp" />

                    <TextView
                        android:id="@+id/text_networks"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/abc_text_size_small_material"
                        />

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

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

        </RelativeLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            app:layout_anchor="@id/card_view"
            app:layout_anchorGravity="right|end|bottom"
            android:layout_marginRight="@dimen/fab_margin">

        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_margin="5dp"

            >
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/image_avatar"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@android:color/holo_red_dark"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

如果想要将图片变成圆形,只需将ImageView更改为de.hdodenhof.circleimageview.CircleImageView即可解决问题。

build.gradle文件中添加以下依赖项,以创建圆形图片。

  compile 'de.hdodenhof:circleimageview:2.0.0

谢谢,但是CardView中的背景图片在哪里? - DolDurma
我只有一张背景图片,在我的两个粘贴图像中,你可以看到它不是“circularImage”。 - DolDurma
@Mahdi.Pishguy 我认为现在你明白了你想要的圆形图像。 - Harshad Pansuriya

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