Various Android Card CardView support results across devices

Playing with Android Support Library v7 widget CardView I see different results on the Galaxy S4 compared to the 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 the following results:

Nexus 4 (5.0.1): enter image description here

Samsung Galaxy S4 (4.4.2): enter image description here

It seems that the one on Nexus calculates the view with its fields, and then draws a shadow from the outside. On the other hand, one of Samsung seems to apply the margins, and then draws a shadow inside until it reaches the calculated viewing boundaries.

Am I missing something?

+5
source share
1 answer

All your observations are correct :)
Everything is well explained by the official CardView documentation :

Prior to L, CardView adds an addition to its content and draws shadows for this area. This fill value is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * corner Radius above and below.

and

Please note that if you specify the exact dimensions for CardView, the shadows, its content area will differ between platforms before L and after L. Using the resource values ​​defined in the api version, you can avoid these changes. Also, if you want CardView to add an inner pad on L platforms and after that, you can set setUseCompatPadding (boolean) to true.

As described here - you should just use setUseCompatPadding (true) , then the external complement on both: L and pre-L will be the same.

+8
source

Source: https://habr.com/ru/post/1211933/


All Articles