drawable中的xml文件讲解

  一般情况下, 除了直接使用放在drawable 目录下的图片, 其实drawable 的用法都与 XML 有关, 使用shape、 layer-list等标签绘制一些背景, 还可以通过selector 标签定义 view 的状态效果。 下面我们就通过几个例子进一步深入 drawable xml文件的学习。

  一、<shape> 标签

<shape>标签用于设定形状,此标签中有几个属性:

属性 属性解释
solid solid用以指定内部填充色心
stroke 这是描边属性,可以定义描边的宽度,颜色,虚实线等
corners shape的圆角属性
padding shape的间隔属性
gradient

gradient用以定义渐变色,可以定义两色渐变和三色渐变,

及渐变样式

size

定义图形大小,基本上不怎么用,因为他所具有的功能

,控件本身也能实现。

首先文档介绍:
 <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
<gradient> shape的颜色渐变属性

attributes:

android:angle
Integer,代表渐变颜色的角度, 0 is left to right, 90 is bottom to top. 必须是45的整数倍. 
默认是 0.该属性只有在type=linear情况下起作用,默认的type为linear。

默认情况下,从左到右:

                                                           

xml代码:<gradient 
        android:startColor="#000000"
        android:endColor="#ffffff"
        />

                                       

angle=270,从上到下 :               

xml代码:<gradient 
        android:startColor="#000000"
        android:endColor="#ffffff"
        android:angle="270"
        />
android:startColor
Color. 颜色渐变的开始颜色,如angle=270中的 android:startColor="#000000"
android:endColor
Color颜色渐变的结束颜色,如angle=270中的   android:endColor="#ffffff"
android:centerColor
Color  颜色渐变的中间颜色,主要用于多彩。
                               
<gradient 
        android:startColor="#000000"
        android:endColor="#ffffff"
        android:centerColor="#ff0000"
        />
android:centerX
Float.(0 - 1.0) 相对X的渐变位置。
android:centerY
Float.(0 - 1.0) 相对Y的渐变位置。  
这两个属性只有在type不为linear情况下起作用。
android:gradientRadius
Float. 渐变颜色的半径,单位应该是像素点. 需要 android:type="radial".
    如果android:type="radial",没有设置android:gradientRadius,将会报错,error inflating class.
      
                                   
xml代码: 
<gradient 
        android:startColor="#ff0000"
        android:endColor="#ffffff"
        android:centerX="0.5"
        android:centerY="0.5"
        android:gradientRadius="30"
        android:type="radial"
        />

加入android:centerColor属性    

           

 <gradient 
        android:startColor="#ff0000"
        android:endColor="#ffffff"
        android:centerColor="#000000"
        android:centerX="0.5"
        android:centerY="0.5"
        android:gradientRadius="30"
        android:type="radial"
        />
android:type
Value Description
"linear" 线性渐变.可以理解为 y=kx+b.
"radial" A radial gradient.圆形渐变,起始颜色从cenralX,centralY点开始。
"sweep" A sweeping line gradient.

  centerX="0.2"   centerX="0.2"     

    

xml代码:      <gradient 
        android:startColor="#ff0000"
        android:endColor="#ffffff"
        android:centerX="0.2"
        android:centerX="0.2"
        android:gradientRadius="30"
        android:type="radial"
        />
 type="sweep":                     

xml代码:

 <gradient 
        android:startColor="#ff0000"
        android:endColor="#ffffff"
        android:centerX="0.5"
        android:centerY="0.5"
        android:type="sweep"

        />

  xml 中的<selector> 标签:点击打开链接

猜你喜欢

转载自blog.csdn.net/qq_41405257/article/details/80456362
今日推荐