我做了一個自定義的多層可繪製來充當按鈕的背景。有時候,我希望這個可繪製圖層的一部分是藍色的。有時我希望它變成綠色。點是,它是一個變量,我希望它可以在關聯的自定義視圖XML中定義。如何在運行時自定義XML中定義的自定義drawable?
這可能嗎?我該如何在XML中編寫一個可以在運行時確定其值的drawable?
custom_button.xml
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/button_inset_horizontal_material"
android:insetTop="@dimen/button_inset_vertical_material"
android:insetRight="@dimen/button_inset_horizontal_material"
android:insetBottom="@dimen/button_inset_vertical_material">
<shape android:shape="rectangle">
<corners android:radius="@dimen/control_corner_material" />
<solid android:color="?attr/colorButtonNormal" />
<padding android:left="@dimen/button_padding_horizontal_material"
android:top="@dimen/button_padding_vertical_material"
android:right="@dimen/button_padding_horizontal_material"
android:bottom="@dimen/button_padding_vertical_material" />
</shape>
</inset>
線<solid android:color="?attr/colorButtonNormal" />
是我想在運行時設置的。我有我的自定義視圖,這個類已經收到了我想在這裏使用的顏色值 - 我如何將它應用到這個drawable的XML中?
不知道是否有在運行時編程修改自定義邏輯的方式。你可能只需要創建兩個custom_button.xml文件,並在需要的時候適當地選擇它們。 – 2015-02-24 14:07:12
@JaySnayder我實際上正在嘗試爲自定義按鈕視圖創建一個庫,使我可以輕鬆修改此可繪製的值使用 – ZakTaccardi 2015-02-24 14:10:36
將圖形提取爲drawable,也許可以更改它:http://stackoverflow.com/questions/7164630 /如何改變形狀 - 動態顏色 – 2015-02-24 14:14:15