shape的四个常用属性及创建步骤

声明:这四个shape的取值是最常使用的,而且复杂程度也是最低的,方便使用。


一、shape的四个常用属性

<!--1.设置渐变 -->
<gradient
        android:startColor="@android:color/white"
        android:centerColor="@android:color/black"
        android:endColor="@android:color/black"
        android:angle="45"  //一般就写这四个属性(开始颜色,中间颜色,结束颜色,渐变角度)(以左下角为渐变的坐标原点)。

        android:type="radial"  //设置渐变的类型,不写默认为线性(linear)。
        android:gradientRadius="90"  //只有当有type时才起作用。

        />


<!-- 2.设置圆角 -->
<corners
        android:radius="9dp" //一般就写这一个属性就行了,下面的四个属性视特殊情况而定。
        android:topLeftRadius="2dp"
        android:topRightRadius="2dp"
        android:bottomLeftRadius="2dp"
        android:bottomRightRadius="2dp"

        />


<!-- 3.设置描边 -->
<stroke
        android:width="2dp" //设置描边的宽度
        android:color="@android:color/black"  //设置描边的颜色

        android:dashWidth="1dp" //设置阴影的宽度大小
        android:dashGap="2dp"  //设置阴影间隙的大小
        />

注:前两个属性一般都是有的,后两个阴影的属性视需求而定。



<!-- 4.设置填充 -->
<solid
        android:color="@android:color/white" //设置填充颜色

      />


二、shape的创建步骤:
1.在res文件的根目录下新建一个drawable文件夹(有就不用新建了)
2.在drawable文件夹下新建一个.xml资源文件(选择shape效果)
3.添加相应的shape属性(最常用的就是我上面说的四个属性,能满足日常开发)
4.在自己的控件中把背景background属性设置为drawable下的对应shape的.xml文件。

 

三、例子代码如下:
shape_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--其中rectagle矩形,oval椭圆,line水平直线,ring环形-->

    <!--圆角-->
    <corners
        android:radius="8dp"/>

    <!--渐变-->
    <gradient
        android:startColor="#cccccc"
        android:centerColor="#ccffdd"
        android:endColor="#cccccc"
        android:angle="90"/>

</shape>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zhiyuan3g.shape.MainActivity">


    <Button
        android:text="hfhahfjk"
        android:textAllCaps="false"
        android:background="@drawable/shape_button"
        android:textColor="@color/color_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_alignParentTop="true"/>

</RelativeLayout>

效果图:




完整Demo代码下载:http://pan.baidu.com/s/1hsbt7rm


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值