【问题标题】:Cardview doesn't respect z index order in relative layoutCardview 不尊重相对布局中的 z 索引顺序
【发布时间】:2014-07-30 19:25:22
【问题描述】:

我有一个相对布局,里面有两个视图,一个 CardView 和一个 ImageButton,我需要将 IB 放在 cardview 上方,但 cardview 不尊重 z 索引顺序。如果我用 LinearLayout 替换 cardview,它似乎没问题,所以我猜问题出在 cardview 本身。

这是我的代码:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/icons_bg_whited"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.noname.classmates.Activity.Register"
tools:ignore="MergeRootFrame"
android:padding="27dip">

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="10dp"
    android:layout_marginTop="38dip">

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

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn_upload_pic"
    android:layout_centerHorizontal="true"
    android:src="@drawable/upload_profile_pic"
    android:contentDescription="@string/upload_profile_picture"
    android:background="@android:color/transparent" /> </RelativeLayout>

【问题讨论】:

    标签: java android android-5.0-lollipop android-cardview


    【解决方案1】:

    在 Android L 上,CardView 有一个高程集,这将使它出现在其他视图之上,无论它们在布局中的顺序如何。您需要在按钮上设置高度,或者更好的是,将按钮放在 CardView 内。

    【讨论】:

    • 另一个问题,我可以设置海拔而不得到阴影吗?
    • 自定义视图阴影和轮廓developer.android.com/training/material/…
    • 似乎是正确的想法。但是,将所有其他可能的视图提升到 cardView 上方根本不起作用......我的 DrawerLayout 仍然显示在 CardViews 下方。
    【解决方案2】:

    是的,问题出在CardView 上,它有一个默认高度,使其显示在任何其他视图之上,而与排序无关。

    为了使其正常化,我最终将 CardView 包裹在 LinearLayout 中。

    那么早,就像

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardCornerRadius="10dp"
        android:layout_marginTop="38dip">
    
    </RelativeLayout>
    
    

    然后我把它改成了,

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
     <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="38dip">
    
      <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardCornerRadius="10dp">
        .
        .
        .
      </CardView>
     </LinearLayout>
    </RelativeLayout>
    
    

    虽然我不知道这样做的确切原因但按预期工作

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多