Подтвердить что ты не робот

layout_margin в CardView не работает должным образом

Я играю с новым CardView, но поля, кажется, не применяются.

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#FFA"
card_view:layout_margind="4dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:id="@+id/info_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

</LinearLayout>

</android.support.v7.widget.CardView>

Кстати, я использую его во фрагменте, который находится в ViewPager. Карта расширяет всю ширину экрана (match_parent), хотя я использую android:layout_marginLeft="18dp" и android:layout_marginRight="18dp" в CardView.

Есть идеи, что я могу делать не так?

4b9b3361

Ответ 2

Если вы используете RecyclerView для добавления CardView, андроида: layout_margin должно быть достаточно.

Но используя ListView для добавления CardView, вы можете сделать это:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="11dp"
    android:layout_marginRight="11dp"
    android:layout_marginTop="11dp">
       (other layout ...)
</android.support.v7.widget.CardView>

</FrameLayout>

Но он обычно не является оптимальным.

Ответ 3

У меня были те же проблемы раньше, я нашел ответ здесь... CardView layout_width = "match_parent" не соответствует родительской ширине RecyclerView

Во время раздувания XML файла CardView внутри вашего ViewPager передайте в него ViewGroup. это позволит надувателю узнать, какие параметры компоновки нужно отнести.

Чтобы раздуть файл макета, используйте что-то вроде ниже:

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_list_item, viewGroup,false);

Надеюсь, что поможет

Ответ 4

Добавьте поля в RecyclerView или ListView вместо "Просмотр карты" или "Макет строки"

Ответ 5

Установите для свойства app: cardPreventCornerOverlap значение false в cardView <android.support.v7.widget.CardView... app:cardPreventCornerOverlap="false"...