Unnecessary padding in CardView?

▼魔方 西西 提交于 2019-11-28 18:37:53

I went through the developer docs again and found that:

On API 20 and before, CardView does not clip the bounds of the Card for the rounded corners. Instead, it adds padding to content so that it won't overlap with the rounded corners.

So for pre-lollipop devices I have to call setPreventCornerOverlap (false); on the cardview.

Update: The same can be done through xml code by adding app:cardPreventCornerOverlap="false" in card view.

thanhbinh84

Setting app:cardPreventCornerOverlap="false" will resolve the problem but also remove corner all pre-lollipop devices. If you want round corner on all devices, add it manually:

card_view_round_corner_background.xml

    <?xml version="1.0" encoding="utf-8"?>      
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">        
    <item>      
    <shape android:shape="rectangle">                
        <solid android:color="@color/transparent"/>                
        <stroke  android:width="2dp" android:color="@color/Black"/>
        </shape>
        </item>
        <item>            
    <shape android:shape="rectangle">
    <solid android:color="@color/transparent"/>             
    <corners android:radius="6dp"/>              
     <stroke  android:width="2dp" android:color="@color/Black"/>           
     </shape>
    </item>
    </layer-list>

In cardview

    <android.support.v7.widget.CardView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"card_view:cardCornerRadius="@dimen/conner_radius"
    card_view:cardBackgroundColor="@color/Black"
    card_view:cardElevation="@dimen/z_card">

    <!-- Put your card contain here -->  

    <View
    android:background="@drawable/card_view_round_corner_border"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

But this solution only works on solid background such as black or white.

Just use FrameLayout instead of LinearLayout, it works

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="-4dp">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="4dp"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardUseCompatPadding="true"

        >

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            >

            <ImageView
                android:id="@+id/imageView"
                android:tag="image_tag"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:src="@drawable/ic_launcher"/>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_weight="2"
                android:orientation="vertical"
                >

                <TextView
                    android:id="@+id/textViewName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="5dp"
                    android:text="Android Name"
                    android:textAppearance="?android:attr/textAppearanceLarge"/>

                <TextView
                    android:id="@+id/textViewVersion"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="5dp"

                    android:text="Android Version"
                    android:textAppearance="?android:attr/textAppearanceMedium"/>

            </LinearLayout>
        </FrameLayout>

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

</FrameLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!