0
當boolean
變爲「true」時,我想更改ImageView
。在這種情況下,如果用戶需要一定的體驗,我想從「普通明星」改爲「黃色明星」。 我實際上有一個類似的代碼,使用ImageButton
。這改變了按鈕被按下時的圖像,但是然後回到了原始圖像。如何在不丟失格式的情況下更改ImageView
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/star_normal"
android:state_pressed="true" />
<item android:drawable="@drawable/star_yellow" />
</selector>
和
<ImageButton
android:layout_width="@dimen/_48sdp"
android:layout_height="@dimen/_48sdp"
android:id="@+id/star"
android:layout_weight="1"
android:layout_marginLeft="@dimen/_15sdp"
app:srcCompat="@drawable/star/>
如何能夠更換ImageView
而不失去在佈局保存位置,DP格式。???不使用按鈕,用戶不應該自己改變圖像。
爲什麼你關心的大小?您嘗試切換的圖像大小不同?如果是這樣,只需指定maxWidth和maxHeight。 –
,因爲通過這種方式可以在不同的屏幕上看到相同比例的圖像,如https://github.com/intuit/sdp – baldcl