Как отцентрировать CardView и получить правильный цвет фона? - PullRequest
0 голосов
/ 24 апреля 2018

У меня есть приложение, которое отображает следующее: uncentered view

Белый прямоугольник - CardView.Вот XML-файл, отображающий действие:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/designerGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    android:columnCount="5"
    android:rowCount="6"
    android:visibility="visible"
    tools:context=".visuals.CloudCardActivity">

    <!-- Left Control Panel (r:0 c:0 r-span:2 c-weight:2) -->
    <LinearLayout
        android:id="@+id/leftControlPanel"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_row="0"
        android:layout_rowSpan="6"
        android:background="#0099cc"
        android:gravity="top|center_horizontal"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textCapCharacters"
            android:text="New" />

    </LinearLayout>

    <!-- Top Control Panel  (r:0 c:1 r-span:1 c-span:3) -->
    <LinearLayout
        android:id="@+id/topControlPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_columnSpan="3"
        android:layout_row="0"
        android:background="#0099cc"
        android:gravity="left|center_vertical"
        android:orientation="horizontal">

        <Spinner
            android:id="@+id/cardFields"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="true"
            android:entries="@array/default_card_fields">

        </Spinner>
    </LinearLayout>

    <!-- Card View Panel  (r:1 c:1 r-weight:8 c-weight:8) -->
    <LinearLayout
        android:id="@+id/cardLayout"
        android:layout_column="2"
        android:layout_columnSpan="3"
        android:layout_row="1"
        android:layout_rowSpan="5"
        android:background="#a3a3a3"
        android:elevation="3dp"
        android:gravity="center_vertical|center_horizontal"
        android:visibility="visible">

        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="560dp"
            android:layout_height="320dp"
            android:background="@android:color/white"
            android:fadingEdge="horizontal|vertical"
            android:focusable="auto"
            android:gravity="center_horizontal"
            android:layoutMode="clipBounds"
            app:cardElevation="5dp" />
    </LinearLayout>

</GridLayout>

Боковая панель: цвет фона LinearLayout также не отображается.

Вот что ожидается: enter image description here

Использую ли я правильные элементы управления для макета?

1 Ответ

0 голосов
/ 25 апреля 2018

попробуйте

<android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="560dp"
            android:layout_height="320dp"
            android:background="@android:color/white"
            android:fadingEdge="horizontal|vertical"
            android:focusable="auto"
            android:layout_gravity="center_horizontal"
            android:layoutMode="clipBounds"
            app:cardElevation="5dp" />

Также прочитайте это -> https://stackoverflow.com/a/3482757/6142219


Альтернативный путь

CardViews обычно используются в Списке элементов, которые должны отображаться внутри ListView или RecyclerView. Если у вас есть только 1 прямоугольник / карта для отображения, я бы порекомендовал использовать пользовательский объект View с закругленным фоном прямоугольника.

Это может быть достигнуто следующим образом.

Создайте XML в вашей папке для рисования "custom_card_bg.xml"

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#ffffff"/> <!-- you can give your own color here -->
    <corners
        android:radius="10dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

Далее, в вашем пользовательском макете скажите «custom_rectangle_layout.xml» в папке / layout, используйте вышеуказанный пользовательский фон

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@drawable/custom_card_bg"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- your layout -->
</LinearLayout>
...