Android自定义shape资源

一、shape资源所在目录:
res/drawable/filename.xml

二、shape资源的引用
In Java: R.drawable.filename
In XML: @[package:]drawable/filename

<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />
-----------------
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);

三、常见标签的使用

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
 <!-- 给图形创造环绕边角,只有 android:shape="rectangle"才有效
            android:radius="2dp"等效于==>( android:topLeftRadius="2dp",android:topRightRadius="2dp"
            ndroid:bottomLeftRadius="2dp" android:bottomRightRadius="2dp")

 --> 
    <corners

        android:topLeftRadius="3dp"
        android:topRightRadius="3dp"
        android:bottomLeftRadius="2dp"
        android:bottomRightRadius="2dp"/>
    <!--给shape一个梯度颜色
        android:startColor="#000000"  开始颜色
        android:endColor="  #00CD00" 结束颜色
        android:centerColor="#7D26CD" 中间颜色
        android:type="sweep" 颜色梯度使用的类型,默认是linear
        android:angle="0" 颜色梯度变化的角度,值必须是45的整数倍 ,否则此标签无效  0(默认)从左到右  90:从上到下      
     -->
    <gradient 
        android:angle="90"
        android:startColor="#FFA07A"
        android:endColor="#7D26CD"/>
    <!-- 设置View的内容的边距
     <padding 
        android:left="60dp"
        android:right="70dp"
        android:top="10dp"
        android:bottom="5dp"/>
     -->

    <!-- 设置shpae大小 
     <size 
        android:width="30dp"
        android:height="37dp"/>
    -->

    <!-- 设置固定的颜色填充shape,这个颜色填满shpae空间
     <solid 
        android:color="#B3EE3A"/>
     -->

     <!-- 分割条
      <stroke 
        android:color="#FF00FF"
        android:width="100dp"
        android:dashGap="7dp"
        android:dashWidth="7dp"/>
         -->
</shape>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值