CardView 角半径 [英] CardView Corner Radius

查看:31
本文介绍了CardView 角半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 CardView 只在顶部有圆角半径?

Is there a way to make CardView only have corner radius at the top?

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp"
    >

推荐答案

除非您尝试扩展 Android CardView 类,否则您无法从 XML 自定义该属性.

Unless you try to extend the Android CardView class, you cannot customize that attribute from XML.

尽管如此,还是有办法获得这种效果的.

Nonetheless, there is a way of obtaining that effect.

将一个 CardView 放在另一个 CardView 中,并将透明背景应用到外部 CardView 并移除其圆角半径 ("cornerRadios= 0dp").例如,您内部的 CardView 将具有 3dp 的cornerRadius 值.然后在你的内部 CardView 上应用一个 marginTop,这样它的底部边界就会被外部的 CardView 切割.这样,您内部 CardView 的底角半径将被隐藏.

Place a CardView inside another CardView and apply a transparent background to your outer CardView and remove its corner radius ("cornerRadios = 0dp"). Your inner CardView will have a cornerRadius value of 3dp, for example. Then apply a marginTop to your inner CardView, so its bottom bounds will be cut by the outer CardView. This way, the bottom corner radius of your inner CardView will be hidden.

XML 代码如下:

 <android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view_outer"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardCornerRadius="0dp"
    card_view:cardElevation="3dp" >

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view_inner"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginTop="3dp"
        card_view:cardBackgroundColor="@color/green"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="0dp" >
    </android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>

视觉效果如下:

始终将您的内容放在您的内部 CardView 中.您的外部 CardView 仅用于隐藏"内部 CardView 的底部圆角.

Always put your content in your Inner CardView. Your outer CardView serves only the purpose of "hiding" the bottom Rounded Corners of the inner CardView.

这篇关于CardView 角半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆