Как принести изображениеView перед картой? Когда оба имеют дочерние элементы Relative 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>
который отлично работает. Результатом будет просмотр изображения над макетом фрейма, но когда я использую следующий xml, в котором я заменяю рамку с помощью 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>
то результатом будет: ImageView на задней панели CardView.
Любое решение для этого?
Еще одно: он отлично работает на устройствах с предварительным леллипопом, но на леденец он не работает.
Выход с CardView на предустановленных устройствах (требуемый вывод)
Выход с CardView на устройствах с леденцами
Ответы
Ответ 1
CardView имеет высоту по умолчанию, которая больше, чем ImageView. Таким образом, мы должны добавить большую высоту в ImageView, чем повышение Карт-карт.
например. imageView.setElevation(10);
и cardView.setElevation(5);
Теперь он работает отлично на всех устройствах.
Ответ 2
Сделайте это так,
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myCardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardBackgroundColor="@android:color/transparent"
card_view:cardElevation="0dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_edit"
android:background="@android:color/transparent"/>
</android.support.v7.widget.CardView>
Он перенесет ImageView
в начало.
Ответ 3
Запишите эту строку в свой Java-код после инициализации ImageView
.
imageView.bringToFront();
Ответ 4
Write Image view Над картой, надеюсь, она решит проблему
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="210dp"
android:src="@drawable/select_delete_chim" />
<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>
</RelativeLayout>
Ответ 5
Вот полный ответ на ваш вопрос
Вы можете обернуть CardView с помощью FrameLayout и поместить ImageView вне родительского элемента CardView следующим образом:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ...>
<FrameLayout...>
<android.support.v7.widget.CardView...>
<Content/>
</android.support.v7.widget.CardView>
</FrameLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
</RelativeLayout>
В качестве альтернативы здесь приведен код Java, который поддерживает версии Android ниже и после леденца на палочке
image.bringToFront();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
image.setElevation(10);
}
это выведет изображение на передний план
Ответ 6
Добавление только app:cardElevation="-1dp"
к CardView
в вашем xml решит проблему.
Ответ 7
простой способ - использовать что-то вроде этого:
<RelativeLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</android.support.v7.widget.CardView>
</LinearLayout>
<ImageView
android:layout_width="wrap_parent"
android:layout_height="wrap_content">
обратите внимание, что это потому, что высота карты. его высота по умолчанию больше, чем ImageView.
таким образом, вы можете использовать LinearLayout, чтобы поместить CardView в него. тогда вы можете использовать RelativeLayout, чтобы сделать ImageView на вершине CardView!
Ответ 8
Вы можете использовать этот атрибут
app:cardElevation="0dp"
в CardView в XMl.
Как это
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardElevation="0dp"
app:cardUseCompatPadding="true">
Это сделает другой вид перед CardView