【问题标题】:CardView goes on top of FrameLayout, but declared firstCardView 位于 FrameLayout 之上,但首先声明
【发布时间】:2015-07-25 17:50:48
【问题描述】:

我有一个简单的 FrameLayout,它支持 CardView 作为第一项,TextView 作为第二项,所以 TextView 必须位于膨胀视图的顶部。这适用于 Lolipop 之前的版本,但在 21+ 卡上占据布局的首位,为什么会这样以及如何解决这个问题?与相对布局相同。 布局:

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

【问题讨论】:

  • 只需将您的文本视图添加到卡片视图中,据我所知,映射在框架布局中未定义 z 顺序,但最后布局的视图应最后绘制。这可能与棒棒糖中的卡片视图使用高程有关,而它们又依赖于棒棒糖之前的边框绘制代码。
  • 事实上,我只需要背景卡和带有阴影的漂亮角落。卡片顶部还有 RecyclerView 与其他卡片,它们的角必须与主卡片重叠。我找到了在 21 岁以上修复它的方法。

标签: android xml android-5.0-lollipop android-support-library android-cardview


【解决方案1】:

如果有人来到这里并且设置海拔的解决方案对他们不起作用(比如在我的情况下,我需要在 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>

【讨论】:

  • 天哪,经过数小时的搜索,您才救了我!干杯老兄!
【解决方案2】:

我可能加入讨论有点晚了,但如果你能负担得起放弃CardView 的海拔,你可以在你的XML 布局中将CardViewcardElevation 属性设置为0dp .

像这样:

app:cardElevation="0dp"

【讨论】:

    【解决方案3】:

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

    这可能与棒棒糖中的卡片视图使用高程有关,而它们依赖于棒棒糖之前的边框绘制代码。

    【讨论】:

    • 是的,你是对的,为 TextView 设置高度可以解决 21+ 的问题。
    • 还有什么问题吗?似乎您可以同时使用棒棒糖和棒棒糖? :)
    • 我的 cardview 设置了 4dp 的高度。我与 matchparent 的框架布局低于卡片视图。将 framelayout 的高度设置为 5dp 并完成。到目前为止没有问题。
    【解决方案4】:

    这对我有用!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多