Android中的Drawable资源—— GradientDrawable

来源:互联网 发布:c4世嘉轮毂数据 编辑:程序博客网 时间:2024/05/10 01:06

ShapeDrawable 根据原始的形状来绘制图形,如矩形,圆形,线条等,可以是一个纯色的色块,也可以渐变的效果。当没有设置具体的图形时,默认为矩形。

<?xml version="1.0" encoding="utf-8"?><shape    xmlns:android="http://schemas.android.com/apk/res/android"    android:shape=["rectangle" | "oval" | "line" | "ring"] >    <corners        android:radius="integer"        android:topLeftRadius="integer"        android:topRightRadius="integer"        android:bottomLeftRadius="integer"        android:bottomRightRadius="integer" />    <gradient        android:angle="integer"        android:centerX="float"        android:centerY="float"        android:centerColor="integer"        android:endColor="color"        android:gradientRadius="integer"        android:startColor="color"        android:type=["linear" | "radial" | "sweep"]        android:useLevel=["true" | "false"] />    <padding        android:left="integer"        android:top="integer"        android:right="integer"        android:bottom="integer" />    <size        android:width="integer"        android:height="integer" />    <solid        android:color="color" />    <stroke        android:width="integer"        android:color="color"        android:dashWidth="integer"        android:dashGap="integer" /></shape>

它对应的是一个GradientDrawable对象。

例如:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <gradient        android:startColor="#FFFF0000"        android:endColor="#80FF00FF"        android:angle="45"/>    <padding android:left="7dp"        android:top="7dp"        android:right="7dp"        android:bottom="7dp" />    <corners android:radius="8dp" /></shape>
<TextView    android:background="@drawable/gradient_box"    android:layout_height="wrap_content"    android:layout_width="wrap_content" />
Resources res = getResources();Drawable shape = res. getDrawable(R.drawable.gradient_box);TextView tv = (TextView)findViewByID(R.id.textview);tv.setBackground(shape);
0 0
原创粉丝点击