android Shape详解(solid,stroke...)

来源:互联网 发布:新手怎么申请淘宝店铺 编辑:程序博客网 时间:2024/04/27 22:58

原文地址 http://blog.csdn.net/feng88724/article/details/6398193


[xhtml] view plaincopyprint?
  1. <shape  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle">  
  4.     <gradient android:startColor="#FFFF0000"  
  5.         android:endColor="#80FF00FF"  
  6.         android:angle="270" />  
  7.     <padding android:left="50dp"  
  8.         android:top="20dp" android:right="7dp"  
  9.         android:bottom="7dp" />  
  10.     <corners android:radius="8dp" />  
  11. </shape>  
 

 

效果:

 

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <shape android:shape="rectangle"  
  3.     xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <solid android:color="#b4000000" />  
  5.     <stroke android:width="2.0dip"  
  6.         android:color="#b4ffffff"  
  7.         android:dashWidth="3.0dip"  
  8.         android:dashGap="0.0dip" />  
  9.     <padding android:left="7.0dip"  
  10.         android:top="7.0dip" android:right="7.0dip"  
  11.         android:bottom="7.0dip" />  
  12.     <corners android:radius="8.0dip" />  
  13. </shape>  
 

 

效果:

 

 

总结一下Shape的所有子属性,如图:

 

 

简单说明一下:

 

gradient   -- 对应颜色渐变。 startcolor、endcolor就不多说了。 android:angle 是指从哪个角度开始变。

solid      --  填充。

stroke   --  描边。

corners  --  圆角。

padding   -- 定义内容离边界的距离。 与android:padding_left、android:padding_right这些是一个道理。

 

 


 

 

上面这些不是本讲重点。 本讲的重点是这些xml中属性,与Java类的对应关系, 以及如何使用Java类写出上面一样的效果!

 

首先来看对应关系:

 

在官方API介绍中:

ShapeDrawable :This object can be defined in an XML file with the <shape> element(这个对象可以用<shape>元素在xml文件中定义)

GradientDrawable :This object can be defined in an XML file with the <shape> element(这个对象可以用<shape>元素在xml文件中定义)


 [父节点] shape   --   ShapeDrawable

 [子节点] gradient   --  

 [子节点] padding   --   

 [子节点] corners   --   setCornerRadius 、setCornerRadii 

 [子节点] solid       --    

 [子节点]  stroke   --   setStroke 

 [子节点]  size --   setSize

 


0 0
原创粉丝点击