[英]How to change shape colors in Drawable?
我有以下按鈕:
<Button
android:id="@+id/Button_Collect"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/CollectButtonShape" />
看起來像這樣:
其中包括以下背景drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:innerRadius="0dp" android:shape="ring" android:thicknessRatio="2" android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="2dp"
android:color="#FF27AE60" />
</shape>
</item>
<item android:top="10dp" android:left="10dp" android:right="10dp" android:bottom="10dp">
<shape android:shape="oval">
<solid android:color="#FF27AE60" />
</shape>
</item>
</layer-list>
如何以編程方式更改環和內圈的顏色(我需要在Touch事件中執行此操作)???
延遲回復,但我發現你可以使用FindDrawableByLayerId ()訪問LayerList中的每一層。 然后我可以訪問每個對象並設置適當的顏色!
通過在drawable中使用不同的圖像是可能的。 考慮一下,如果要將綠色更改為黑色,則需要在drawable中使用黑色圓圈然后嘗試此代碼。 這會對你有所幫助..
在按鈕單擊事件上,使用此代碼
public void onClick(View v) {
if(v == ButtonName) {
ButtonName.setImageResource(R.drawable.ImageName);
}
}
您可以嘗試使用ColorStateList ,我認為它符合您的目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.