Android Shape Drawable Resources

本文主要介绍Drawable Resources的一种,Shape Drawable Resources的使用。其他Drawable类似

经常需要自己设置某个view的背景,比如类似新浪微博客户端微博源内容的灰底圆角效果,这个时候我们就可以使用Shape。

 

1、介绍

Shape Drawable Resources是指一个XML文件,它定义了几何形状,包括颜色和渐变

放在res/Drawable文件夹下,文件名即为资源id,可以在其他layout中调用R.drawable.filename,

对应的类为ShapeDrawable

shape包含矩形、椭圆形、行、环形。

 

2、使用

下面以为一个TextView设置一个渐变色的边框为例进行介绍,第三部分对具体属性含义进行介绍

2.1 定义一个渐变色的矩形shape,文件路径res/drawable/gradient_box.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle">  
  4.     <gradient  
  5.         android:startColor="#FFFF0000"  
  6.         android:endColor="#80FF00FF"  
  7.         android:angle="45"/>  
  8.     <padding android:left="7dp"  
  9.         android:top="7dp"  
  10.         android:right="7dp"  
  11.         android:bottom="7dp" />  
  12.     <corners android:radius="8dp" />  
  13. </shape>  

2.2 TextView属性设置

Xml代码   收藏代码
  1. <TextView  
  2.     android:background="@drawable/gradient_box"  
  3.     android:layout_height="wrap_content"  
  4.     android:layout_width="wrap_content" />  

 其中 android:background="@drawable/gradient_box"表示设置背景为 gradient_box 这个drawable

或者在后台程序中设置

Java代码   收藏代码
  1. Resources res = getResources();  
  2. Drawable shape = res. getDrawable(R.drawable.gradient_box);  
  3.   
  4. TextView tv = (TextView)findViewByID(R.id.textview);  
  5. tv.setBackground(shape);  

 

3、属性介绍

xml定义如下

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:shape=["rectangle" | "oval" | "line" | "ring"] >  
  5.     <corners  
  6.         android:radius="integer"  
  7.         android:topLeftRadius="integer"  
  8.         android:topRightRadius="integer"  
  9.         android:bottomLeftRadius="integer"  
  10.         android:bottomRightRadius="integer" />  
  11.     <gradient  
  12.         android:angle="integer"  
  13.         android:centerX="integer"  
  14.         android:centerY="integer"  
  15.         android:centerColor="integer"  
  16.         android:endColor="color"  
  17.         android:gradientRadius="integer"  
  18.         android:startColor="color"  
  19.         android:type=["linear" | "radial" | "sweep"]  
  20.         android:useLevel=["true" | "false"] />  
  21.     <padding  
  22.         android:left="integer"  
  23.         android:top="integer"  
  24.         android:right="integer"  
  25.         android:bottom="integer" />  
  26.     <size  
  27.         android:width="integer"  
  28.         android:height="integer" />  
  29.     <solid  
  30.         android:color="color" />  
  31.     <stroke  
  32.         android:width="integer"  
  33.         android:color="color"  
  34.         android:dashWidth="integer"  
  35.         android:dashGap="integer" />  
  36. </shape>  

其中shape必须为根元素,android:shape定义了形状,默认为矩形。

corners只对矩形有效,表示圆角的度数

gradient表示渐变色

padding表示即对内的偏移

size为shape大小

solid为填充色

stroke为shape边线的设置

以上属性的自属性含义见drawable-Shape,更多可以使用的属性见GradientDrawable.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值