Playing with Android Support Library
v7 widget CardView
I see different results on a Galaxy S4 comparing to a Nexus 4 device. Having the following layout:
<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_width="fill_parent"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal"
card_view:cardCornerRadius="7dp"
card_view:cardElevation="12dp">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true">
<TextView
android:id="@+id/txtExample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_messages" />
</ScrollView>
</android.support.v7.widget.CardView>
I got these results:
Nexus 4 (5.0.1):
Samsung Galaxy S4 (4.4.2):
It seems that the one on Nexus calculates the View with it's margins, and then the paints the shadow outside. On the other hand, the one of Samsung seems to apply margins and then paint the shadow inside until it reaches the calculated View's bounds.
Am I missing something?
CardView with consistent look/UI on all Android platforms Android CardView have different padding/size on pre-Lollipop and post-Lolipop devices. On KitKat devices and above (API v21+), there is no outer padding between 2 CardView CardView adds additional padding to draw shadows on platforms before Lollipop.
This library adds support for the CardView widget, which lets you show information inside cards that have a consistent look on any app. These cards are useful for material design implementations, and are used extensively in layouts for TV apps. The Gradle build script dependency identifier for this library is as follows:
In order to use any of the following libraries, you must download the library files to your Android SDK installation. Follow the directions for downloading the Support Libraries in Support Library Setup to complete this step. You must take additional steps to include a specific Support Library in your application.
CardView - A support library custom class for creating Material Design style display cards. This class is based on FrameLayout with rounded corners and a drop shadow. AppCompatDialogFragment - Provides a consistently styled dialogs by extending DialogFragment and using AppCompatDialog .
All your observations are correct :)
Everything is explained well on official documentation of CardView:
Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.
and:
Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before L and after L. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms L and after as well, you can set setUseCompatPadding(boolean) to true.
As described there - you should just use setUseCompatPadding (true)
then outer padding on both: L and pre-L will be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With