2017-01-10 113 views
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格式。???不使用按鈕,用戶不應該自己改變圖像。

+0

爲什麼你關心的大小?您嘗試切換的圖像大小不同?如果是這樣,只需指定maxWidth和maxHeight。 –

+0

,因爲通過這種方式可以在不同的屏幕上看到相同比例的圖像,如https://github.com/intuit/sdp – baldcl

回答

0

沒有選擇的粉絲,所以我會做編程,像這樣:

if (boolean){ 
    imageView.setImageResource(R.drawable.star_yellow); 
} 

爲了避免調整指定的ImageView這樣的XML

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/img" 
    android:src="@drawable/star_normal" 
    android:maxHeight="48dp" 
    android:maxWidth="48dp"/> 
相關問題