CardView位于FrameLayout之上,但首先声明

Kon*_*kov 27 xml android android-support-library android-cardview android-5.0-lollipop

我有简单的FrameLayout,支持CardView作为第一项,TextView作为第二项,因此TextView必须位于膨胀视图的顶部.这适用于Lolipop之前,但21+卡在布局中占据最高位置,为什么会这样,以及如何解决这个问题?与RelativeLayout相同.布局:

<?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="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="20dp"
        app:cardBackgroundColor="#ff0000"
        app:cardUseCompatPadding="false"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="i am top view!"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:textAllCaps="true"
        android:textColor="#00ff00"
        android:background="#0000ff"
        />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

Ere*_*eza 44

如果有人到达这里并且设置高程的解决方案对他们不起作用(就像在我的情况下,我需要在其上方绘制图像CardView并且在其上有阴影是不可接受的),您可以通过包装来解决问题在CardView里面另一个FrameLayout.在提供的示例中,它看起来像这样:

<?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="wrap_content">

    <!-- This is the added FrameLayout -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_margin="20dp"
            app:cardBackgroundColor="#ff0000"
            app:cardUseCompatPadding="false"
            />

    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="i am top view!"
        android:gravity="center"
        android:layout_gravity="center"
        android:textSize="30sp"
        android:textAllCaps="true"
        android:textColor="#00ff00"
        android:background="#0000ff"
        />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)


Joh*_*gun 16

只需在卡片视图中添加文本视图,我知道在框架布局中未定义z顺序,但最后布局视图应该最后绘制.

这可能与棒棒糖使用高程中的卡片视图有关,而它们在棒棒糖前面的边框绘制代码上回落.

  • 是的,您说得对,为 TextView 设置高程可以解决 21 岁以上的问题。 (2认同)

Bik*_*ram 8

这对我有用!

<?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="wrap_content">

    <!-- Add this layout as parent of CardView -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_margin="20dp"
            app:cardBackgroundColor="#ff0000"
            app:cardUseCompatPadding="false" />

    </LinearLayout>
    <!--Close parent layout-->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="#0000ff"
        android:gravity="center"
        android:text="i am top view!"
        android:textAllCaps="true"
        android:textColor="#00ff00"
        android:textSize="30sp" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)


Ita*_*mar 8

我可能会迟到加入讨论,但是如果你能放弃CardView升高,你可以将XML布局中的cardElevation属性设置CardView0dp.

像这样:

app:cardElevation="0dp"
Run Code Online (Sandbox Code Playgroud)