Android 可繪制資源

2018-02-18 15:38 更新

在Drawable中創(chuàng)建圓角

要在Drawable中實(shí)現(xiàn)圓角,可以使用當(dāng)前未記錄的<shape> 標(biāo)簽。

這個(gè)標(biāo)簽需存在于 /res/drawable 目錄的一個(gè)文件中。

以下代碼顯示了如何使用<shape> 標(biāo)簽在名為 /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類(lèi)。drawable資源值,如一個(gè)矩形,解析為ColorDrawable

帶有< shape> 標(biāo)簽的XML文件解析為 GradientDrawable 。



以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)