强大的SHAPE资源

来源:互联网 发布:java 单引号 转义 编辑:程序博客网 时间:2024/04/30 11:14

shape资源可以定义形状、渐变、边距、边框、圆角(你越来越像一头猪了——“形渐变变圆” )
今使用shape资源实现一个圆角矩形风格的TextView 

定义shape资源:
<?xml version="1.0" encoding="utf-8"?>
<!--shape属性的可选项还有:line,ring,oval-->
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <!--45度渐变色-->
    <gradient android:startColor="#ffff0000" android:endColor="#80ff00ff" android:angle="45"/>
    <padding android:top="7dp" 
android:bottom="7dp"  android:left="7dp"  android:right="7dp" />
    <stroke android:width="2dp" android:color="#ffffff"/>
    <!--圆角半径8-->
    <corners android:radius="8dp"/>
</shape> 

在布局中使用shape:
<TextView
android:id="@+id/tv_shape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text="what a shape!"/> 

在代码中动态使用:
Drawable shape=getResources().getDrawable(R.drawable.shape); 
TextView tv=findViewById(R.id.tv_shape) ;
tv.setBackground(shape); 
0 0
原创粉丝点击