Andorid之使用ImageView和Selector配置文件制作多样式按钮(默认、点击、灰掉)

     在做地图的时候  有个需求:

     当可以缩放地图的时候,放大按钮可以使用。当不可以使用地图的时候,放大按钮不可以使用。放大按钮在默认状态下是一个背景,点击的状态下是另一个背景。

     废话少说,代码奉上:

    一、ImageView配置文件片段。

        

   <ImageView
    	android:id="@+id/ivZoomOut"
    	android:layout_height="wrap_content"
    	android:layout_width="wrap_content"
    	android:layout_marginRight="10px"
    	android:layout_marginBottom="10px"
    	android:src="@drawable/zoom_out_2"
    	/>

 二、样式控制配置文件zoom_out_2.xml:

  

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">     
<!-- Disabled -->
	<item android:state_enabled="false" 
	      android:state_pressed="false" 
	      android:textColor="@color/red"
		  android:drawable="@drawable/zoom_out_disable" />     
<!-- Pressed -->
	<item android:state_enabled="true" 
	      android:state_pressed="true"
		  android:textColor="@android:color/white" 
		  android:drawable="@drawable/zoom_out_pressed" />     
<!-- Focused -->
<!-- 	<item android:state_enabled="true" 
	      android:state_focused="true"
		  android:textColor="@android:color/white" 
		  android:drawable="@drawable/list_item_focused" /> -->     
<!-- Default -->
	<item android:state_enabled="true" 
	      android:state_pressed="false" 
	      android:drawable="@drawable/zoom_out_default"/>
</selector> 

 三、代码控制:

  

 if(**){
           zoomOutView.setEnabled(false);
     }else{
          zoomOutView.setEnabled(true);
     }

 

posted @ 2012-05-24 15:35  李克华  阅读(2541)  评论(2编辑  收藏  举报