Android 可绘制资源

2018-02-18 15:38 更新

在Drawable中创建圆角

要在Drawable中实现圆角,可以使用当前未记录的<shape> 标签。

这个标签需存在于 /res/drawable 目录的一个文件中。

以下代码显示了如何使用<shape> 标签在名为 /res/drawable/my_rounded_rectangle.xml 的文件中定义圆角矩形。

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#f0600000"/>
    <stroke android:width="3dp" color="#ffff8080"/>
    <corners android:radius="13dp" />
    <padding android:left="10dp" 
             android:top="10dp"
             android:right="10dp" 
             android:bottom="10dp" />
</shape>

然后,你可以使用此可绘制资源作为之前文本视图的背景,例如。

GradientDrawable roundedRectangle = (GradientDrawable)
activity.getResources().getDrawable(R.drawable.my_rounded_rectangle);

textView.setBackgroundDrawable(roundedRectangle);

最后,drawable子目录中的位图图像解析为BitmapDrawable类。drawable资源值,如一个矩形,解析为ColorDrawable

带有< shape> 标签的XML文件解析为 GradientDrawable



以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号