Android 在 RelativeLayout 中将 Cardview 放在父 Cardview 的顶部

Android put Cardview top of parent Cardview in RelativeLayout

如何在下面的屏幕截图中将 cardview 放在父级 cardview 之上?

当我把图像放到 CardView 上时,我的观点被破坏了

正确的观点必须是:

我的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>

框架和协调器布局的工作方式相同:第一个 View 是最背景,下一个 View 位于前一个的顶部。

所以我认为你应该在 xml.

中更改 CardViews 的顺序

您也可以尝试使用 android:elevation 以获得理想的结果。

经过一些尝试,我实现了 such result:

我的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>

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

如果你喜欢这种类型的图片

然后使用此代码。

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>

更新日期:

我想你需要这个

代码:

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

要在圆形中更改 Image 只需将 ImageView 更改为 de.hdodenhof.circleimageview.CircleImageView 即可解决您的问题。

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

  compile 'de.hdodenhof:circleimageview:2.0.0

CardView 的默认 cardElevation=4。因此,进入该汽车视图的同一父布局内的所有其他视图都将位于该卡片视图下方。

在您的代码中,由于默认的 cardElevation,id 为 'malam' 的卡片视图将位于其他视图之上。尝试将其 cardElevation 设置为 0dp 以覆盖它。

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

UPDATE

Setting 0dp will remove the shadow of that cardview. If shadow is required, then either

  1. Remove cardElevation property from CardView "malam" & set "android:elevation" to the views with id "linearLayout3" & "robot_dialog_icon" so that value is greater than 4dp.
    (Note : This works for lollipop & higher versions only)

    OR

  2. Wrap "linearLayout3" & "robot_dialog_icon" inside individual CardViews.