6

我正在尝试在 cardview 中创建一个应用程序,cardview 包含两个文本视图,它们需要彼此相邻放置,但文本视图正在重叠。是否有任何机制可以将元素放置在 Cardviews 的 RelativeLayout(layout_below 或 layout_toRightOf) 中。我可以通过向 Relativelayout 添加阴影并使其看起来像 Cardview 来做到这一点,我想在 Cardview 中执行此操作。

我的布局代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@android:color/black"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:background="@color/white"
        android:layout_width="200dp"
        android:layout_height="200dp">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Textview one"
            android:gravity="center"
            android:textSize="36sp" />
        <TextView
            android:id="@+id/info_text1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Textview two"
            android:gravity="center"
            android:textSize="36sp" />
    </android.support.v7.widget.CardView>
</RelativeLayout>

目前显示如下图所示:

在此处输入图像描述

4

1 回答 1

17

From the description of CardView

CardView extends the FrameLayout class and lets you show information inside cards that have a consistent look on any app. CardView widgets can have shadows and rounded corners.

As you probably know, in FrameLayout there are no constraints or relations between views positioning. They will be displayed one above each other. If you want to use different hierarchy inside a CardView please just use any layout you want inside. For example:

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:background="@color/white"
    android:layout_width="200dp"
    android:layout_height="200dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/info_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Textview one"
            android:gravity="center"
            android:textSize="36sp" />
        <TextView
            android:id="@+id/info_text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Textview two"
            android:gravity="center"
            android:textSize="36sp" />
    </LinearLayout>

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

Treat the CardView only as an outer decoration and you can use ANY layout inside. So if LinearLayout is not appropriate you can use RelativeLayout etc.

于 2014-08-26T11:01:19.030 回答