2011-06-28 147 views
0

嗨我已經創建了一個相對佈局。並將imageview對齊到relativelayout的右側。我使用選擇器設置了relativelayout的背景。如果我選擇thye relativelayout它的顏色將是綠色,否則它的顏色將是白色。現在我想根據scrollview的選擇來設置圖像。如果scrollview被選中,那麼我想顯示imge1,如果沒有,那麼我想顯示image2。我怎樣才能做到這一點?什麼是最好的方式來設置圖像imageview

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/selector"> 
    <ImageView 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" 

     android:layout_alignParentRight="true" /> 
</RelativeLayout> 

selector.xml

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_focused="true" 
     android:state_pressed="false" 
     android:drawable="@drawable/grbk" /> 
    <item 
     android:state_focused="true" 
     android:state_pressed="true" 
     android:drawable="@drawable/grbk" /> 
    <item 
     android:state_focused="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/grbk" /> 
    <item 
     android:drawable="@drawable/whbk" /> 
</selector> 

感謝 蘇尼爾enter image description here

回答

0

我們可以爲imageview創建另一個選擇器,並將該選擇器作爲背景傳遞給imageview。

imageselector.xml

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_focused="true" 
     android:state_pressed="false" 
     android:drawable="@drawable/arow_selected" /> 
    <item 
     android:state_focused="true" 
     android:state_pressed="true" 
     android:drawable="@drawable/arow_selected" /> 
    <item 
     android:state_focused="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/arow_selected" /> 
    <item 
     android:drawable="@drawable/arow_unselect" /> 
</selector> 

main.xml中

 <ImageView 
     android:id="@+id/arrow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/imageselector" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="20dip"/> 
</RelativeLayout> 
+0

我試過了。它也適用於我 –

1

以同樣的方式作爲背景。您應該添加機器人:duplicateParentState =「真」

<ImageView 
     android:duplicateParentState="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/arrow_selector" 
     android:layout_alignParentRight="true" /> 
0

第一顏色設置爲所有的清單列表視圖項,然後放置在列表視圖中..我GESS選擇你知道如何設置選擇在列表視圖中

0

這裏是我的建議.. 一旦我設定背景的ImageView的,然後當我想改變透明度,我什麼都不能做......在查看一些網站之後,我發現我需要設置imageview的資源代替背景...

相關問題