《第一行代码》阅读笔记(十六)——Shape

在Android开发中,我们可以使用shape定义各种各样的形状,也可以定义一些图片资源。相对于传统图片来说,使用shape可以减少资源占用,减少安装包大小,还能够很好地适配不同尺寸的手机。
不多说,直接上一个简单的案例:
在drawble文件定义shape_round_corner.xml

<?xml version="1.0" encoding="utf-8"?><!--    //定义四个圆角-->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ff00" />
    <corners
        android:bottomLeftRadius="12dp"
        android:bottomRightRadius="12dp"
        android:topLeftRadius="12dp"
        android:topRightRadius="12dp" />
    <stroke
        android:width="1dp"
        android:color="#ff0" />
</shape>

在需要的时候直接通过背景属性引入即可。

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="15dp"
        android:layout_weight="5"
        android:background="@drawable/shape_round_corner">
    </RelativeLayout>

渐变案例

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="30dp" />

    <gradient
        android:angle="180"
        android:endColor="#42B0E9"
        android:gradientRadius="50%p"
        android:startColor="#467FE5"
        android:type="linear" />
</shape>

效果

参考文件
Android shape属性大全

原文地址:https://www.cnblogs.com/zllk/p/13369681.html