3.6 Drawable样式装饰
1、ColorDrawable
ColorDrawable:在res/values/color.xml中添加颜色值,然后@color/颜色名获取资源;
示例:color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<!--十六进制颜色值-->
<color name="LightPink">#FFB6C1</color>
<!--一般颜色值是十六进制6位,如果加入透明度,00~FF加入开始两位-->
<color name="BlackFF">#FF000000</color>
</resources>
XML布局文件中使用颜色
示例:
<!--java中设置颜色-->
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"/>
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"/>
<!--使用color.xml中颜色-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"
android:textColor="@color/colorAccent"/>
<!--使用android自带颜色-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"
android:textColor="@android:color/holo_orange_dark"/>
Activity中设置颜色
示例:
public class ColorDrawableActivity extends Activity {
//上下文
private Context mContext;
private TextView textView;
private TextView textView1;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.learn_color_drawable);
mContext = ColorDrawableActivity.this;
textView = findViewById(R.id.tv_1);
textView1=findViewById(R.id.tv_2);
//设置颜色,自定义,需要0x开头,透明度,6位颜色值
int color = 0xff008577;
textView.setBackgroundColor(color);
//获取color.xml中颜色
int color1=getResources().getColor(R.color.colorAccent);
//获取系统颜色
int color2= Resources.getSystem().getColor(android.R.color.holo_blue_dark);
textView1.setBackgroundColor(color2);
}
}
2、NinepatchDrawable
NinepatchDrawable:点9图,图片:图片名.9.png等,使用NinePatchEditor工具编辑获得图片资源;
示例:drawable下创建xml资源文件
<?xml version="1.0" encoding="utf-8"?>
<!--
点9图drawable资源
-->
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/icon_v"
android:dither="true">
</nine-patch>
Layout布局文件使用.9图
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/draw_ninepatch_1"/>
3、ShapeDrawable
ShapeDrawable:形状Drawable资源,包括直线,边框,环形,方形,椭圆等;
① <shape>:
~ visible:设置是否可见
~ shape:形状,可选:rectangle(矩形,包括正方形),oval(椭圆,包括圆),line(线段),ring(环形)
~ innerRadiusRatio:当shape为ring才有效,表示环内半径所占半径的比率,如果设置了innerRadius, 他会被忽略