10.6.11 外形(Shape)资源
外形资源是一种非常有意思,也非常强大的资源。通过外形资源,可以为控件加上渐变背景色;使控件的四个角变成圆形;设置控件内容到控件边界的距离等。
外形资源使用标签中的子标签定义各种效果。例如,res/drawable/shape.xml文件中定义了渐变色、控件内容距离边界的距离、圆角和边框线。
源代码文件:src/ch10/Drawable/res/drawable/shape.xml
android:shape="rectangle">
androidandroid:endColor="#80FF00FF"android:angle="45"/>
定义外形资源时,需要使用标签的android:shape属性指定要绘制的形状。在本例中该属性值为rectangle,表示绘制矩形。android:shape属性可以指定如下4个值。
rectangle:矩形。
oval:椭圆。
line:直线。
ring:圆环。
下面的代码在标签中使用了shape.xml资源文件。
源代码文件:src/ch10/Drawable/res/layout/shape.xml
android:layout_height="wrap_content"android:layout_width="wrap_content"
android:layout_margin="20dp"android:text="Shape Label"/>
也可以使用下面的代码设置TextView控件的背景色。
Resourcesres=getResources();
Drawableshape=res. getDrawable(R.drawable.shape);
TextViewtv= (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
现在运行程序,会显示如图10-28所示的效果。
【责任编辑:book TEL:(010)68476606】
点赞 0