11

我有一个相对布局,里面有两个视图,一个 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>
4

2 回答 2

18

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

于 2014-07-30T20:41:30.527 回答
1

是的,问题在于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>

虽然我不知道这有效但按预期工作的确切原因。

于 2020-07-23T05:45:06.357 回答