Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define margin of CardView

What is the default margin of Support CardView so I can define margin for my situation?

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="2dp">
like image 222
Kyryl Zotov Avatar asked Apr 20 '17 16:04

Kyryl Zotov


People also ask

What is cardview in Material Design?

CardView was introduced in Material Design in API level 21 (Android 5.0 i.e Lollipop). CardView uses elevation property on Lollipop for shadows and falls back to a custom emulated shadow implementation on older platforms. This new widget is a big step for displaying data/information inside cards.

How do I use a cardview in a layout?

In order to use the `CardView` you need to add it to your layout file. Use it as a view group to contain other views. In this example, the `CardView` contains a single TextView to display some information to the user.

What is a view card?

These cards have a default elevation above their containing view group, so the system draws shadows below them. Cards provide an easy way to contain a group of views while providing a consistent style for the container. Figure 1. Card examples

What is the cardview API?

The system provides the CardView API as an easy way for you to show information inside cards that have a consistent look across the platform. These cards have a default elevation above their containing view group, so the system draws shadows below them.


2 Answers

I also came up with the same problem. It didn't work even though I added android:layout_marginBottom .

But I found the solution. Your CardView should be inside the RelativeLayout. Then android:layout_marginBottom and android:layout_marginTop will work as expected.

like image 171
AlexM Avatar answered Oct 07 '22 14:10

AlexM


How to define margin of CardView?

Use attribute android:layout_marginLeft, android:layout_marginRight, android:layout_marginTop and android:layout_marginBottom to define CardView margins.

What is the default margin of Support CardView?

Here is the CardView Documentation. Also see the Design Guidelines.

FYI, Use attribute card_view:cardUseCompatPadding="false" to remove inner padding inside CardView.

like image 32
Ferdous Ahamed Avatar answered Oct 07 '22 16:10

Ferdous Ahamed