drawable这个目录要和drawable-hdpi....这些适配目录进行区分,在开发中,里面主要放置一些xml文件
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_active=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false”]
一.shape,在没有美工的帮助或显示简单图形的时候,可以用到,而里面定元素有4个:line,oval,ring,rectangle,通过android:shape=“”来设置。
1.line,主要是使用stroke标签来完成绘制一条线,而直线的主要作用是分隔两个控件。
这里简单介绍绘制直线和虚线。
绘制直线:在stroke标签中定义宽度就可以了,然后设置颜色
绘制虚线:在stroke标签中定义dashWidth和dashGap,然后还需要关闭当前activity的硬件加速,否则虚线不显示只显示直线。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="2dp"
android:color="#ffff0000"
android:dashWidth=“3dp”
android:dashGap="4dp"
/>
</shape>
关闭activity中的硬件加速
android:hardwareAccelerated="true"
2.oval椭圆
a.完成一个简单的椭圆,使用size和solid来设置外接矩形和固定颜色
b.完成一个有外环描边的椭圆,使用stroke来完成,实线和虚线不用关闭硬件加速了.
c.渐变颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!-- 一个简单的椭圆 -->
<size
android:width="50dp"
android:height="50dp" />
<!-- 渐变颜色 -->
<!--从中心向外部扩散-->
<gradient
android:centerColor="#ffff0000"
android:gradientRadius="50dp"
android:endColor="#ff0000ff"
/>
<!--从左向右-->
<gradient
android:startColor="#ffff0000"
android:endColor="#ff0000ff"
/>
<!--可以调整角度-->
<gradient
android:startColor="#ffff0000"
android:endColor="#ff0000ff"
android:angle="90"
/>
<gradient
android:centerX="50%"
android:centerY="50%"
android:centerColor="#ffff0000"
/>
<!-- 苗边,可以是实线或虚线 -->
<stroke
android:width="5dp"
android:color="#ffff0000"
android:dashGap="5dp"
android:dashWidth="5dp" />
</shape>
3.rectagle,用来当纯色背景圆角经常用到
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--圆角-->
<corners android:radius="10dp" />
<!---->
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>
<solid android:color="#ff00ffff"/>
<stroke android:color="#ffff0000" android:width="2dp"/>
</shape>
4.ring,控件的大小一定要比xml中的宽度大,否则显示不全或不显示(实际显示到外面被剪裁了),控件大小一般为width + thickness * 2 + innerRadius
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="1dp"
android:thickness=“1dp"
android:useLevel="false">
<stroke
android:width="60dp"
android:color="#ff00fff0" />
</shape>
二.下面我们看下selector主要是对控件的一些状态显示进行设置。
drawable的item中可以有以下属性:
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_active=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false”]
拿
android:state_pressed为例,当设置它的drawable时,按下状态就是相应的drawable效果。当然,也可以在item标签下使用shape。
三.level_list这个在开发过程中也可能经常用到简单讲解下
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="1" android:drawable="@drawable/example_shape_rect"/>
<item android:maxLevel="3" android:drawable="@drawable/example_shape_ring"/>
</level-list>
主要用于ImageView,然后通过ImageView的setImageLevel来显示,其中的显示的drawable会根据设置的值来显示,显示这个指定值或更大的值所对应的图像。比如会员等级或身份可以设置对应的常量值来显示响应的图片。