问题描述
我有一个布局XML:
<?xml version = 1.0 encoding = utf-8? >
< RelativeLayout xmlns:android = http://schemas.android.com/apk/res/android
xmlns:tools = http://schemas.android.com/tools
android:id = @ + id / containor
android:layout_width = match_parent
android:layout_height = match_parent
android:background =#fff6da
android:paddingBottom = @ dimen / activity_vertical_margin
android:paddingLeft = @ dimen / activity_horizontal_margin
android:paddingRight = @ dimen / activity_horizontal_margin
android:paddingTop = @ dimen / activity_vertical_margin
tools:context = com.vats.vatishs.cardviewprogrammatically.MainActivity>
< FrameLayout
android:id = @ + id / card
android:layout_width = 220dp
android:layout_marginTop = 10dp
android:layout_height = wrap_content>
< TextView
android:background = @ color / white
android:layout_width = match_parent
android:layout_height = match_parent
android:padding = 10dp
android:text =你好,我是文本视图。 />
< / FrameLayout>
< ImageView
android:layout_width = wrap_content
android:layout_height = wrap_content
android:layout_marginLeft = 210dp
android: background = @ drawable / select_delete_chim />
< / RelativeLayout>
这很好用。结果将是框架布局上方的图像视图,但是当我使用以下xml将CardLayout替换为框架布局时:
<?xml version = 1.0 encoding = utf-8?>
< RelativeLayout xmlns:android = http://schemas.android.com/apk/res/android
xmlns:tools = http://schemas.android.com/tools
android:id = @ + id / containor
android:layout_width = match_parent
android:layout_height = match_parent
android:background =#fff6da
android:paddingBottom = @ dimen / activity_vertical_margin
android:paddingLeft = @ dimen / activity_horizontal_margin
android:paddingRight = @ dimen / activity_horizontal_margin
android:paddingTop = @ dimen / activity_vertical_margin
tools:context = com.vats.vatishs.cardviewprogrammatically.MainActivity>
< android.support.v7.widget.CardView
android:id = @ + id / card
android:layout_width = 220dp
android :layout_marginTop = 10dp
android:layout_height = wrap_content>
< TextView
android:background = @ color / white
android:layout_width = match_parent
android:layout_height = match_parent
android:padding = 10dp
android:text =你好,我是文本视图。 />
< /android.support.v7.widget.CardView>
< ImageView
android:layout_width = wrap_content
android:layout_height = wrap_content
android:layout_marginLeft = 210dp
android: background = @ drawable / select_delete_chim />
< / RelativeLayout>
然后结果将是:CardView背面的ImageView。
有什么解决方案吗?
还有一点,它可以在棒棒糖之前的设备上正常工作,但在棒棒糖上则无法工作。 / p>
CardView的默认海拔高度大于ImageView,因此我们必须添加更大的海拔高度ImageView中的高度比CardView中的高度。
例如 imageView.setElevation(10);
和 cardView.setElevation(5);
现在,它可以在所有设备上正常工作。
I have a layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/containor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff6da"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.vats.vatishs.cardviewprogrammatically.MainActivity">
<FrameLayout
android:id="@+id/card"
android:layout_width="220dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:text="Hello I am a text View." />
</FrameLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="210dp"
android:background="@drawable/select_delete_chim" />
</RelativeLayout>
which works fine. The result will be a image view above frame layout but when I use the following xml in which i have replace frame layout with CardView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/containor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff6da"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.vats.vatishs.cardviewprogrammatically.MainActivity">
<android.support.v7.widget.CardView
android:id="@+id/card"
android:layout_width="220dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:text="Hello I am a text View." />
</android.support.v7.widget.CardView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="210dp"
android:background="@drawable/select_delete_chim" />
</RelativeLayout>
then the result will be: ImageView at the back of CardView.
Any Solution for this?
One thing more, It will work fine on pre-lollipop devices but on lollipop it doesn't work.
Output with CardView on pre-lollipop devices (required output)
Output with CardView on lollipop devices
CardView has default elevation which is greater than ImageView, So we have to add greater elevation in ImageView than CardView's elevation.
e.g. imageView.setElevation(10);
and cardView.setElevation(5);
Now, its working fine on all devices.
这篇关于如何将imageView放在cardview的前面?当两者都是相对布局子项时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!