当前位置:操作系统 > 安卓/Android >>

Android ApiDemos示例解析(123):Views->ImageButton

ImageButton 为ImageView的子类,因此可以显示一个图形,同时它具有Button的功能,能够被按下并响应用户点击事件。ImageButton缺省显示和Button同样风格。可以在Layout文件中通过android:src 或是在代码中使用setImageResource(int) 为Button指定图像。

ImageButton可以为ImageButton重新设置背景图像。并可以为Button的不同状态(获取焦点,失去焦点,按下)指定不同的图像,比如,绿色的图像作为缺省显示,按下时显示黄色图像,获取焦点时显示橙色。一个简单的方法是使用”selector” drawable 资源,比如:

[html] 
<?xml version=”1.0″ encoding=”utf-8″?> 
 <selector xmlns:android=”http://schemas.android.com/apk/res/android”> 
 <item android:state_pressed=”true” 
 android:drawable=”@drawable/button_pressed” /> <!– pressed –> 
 <item android:state_focused=”true” 
 android:drawable=”@drawable/button_focused” /> <!– focused –> 
 <item android:drawable=”@drawable/button_normal” /> <!– default –> 
 </selector> 

<?xml version=”1.0″ encoding=”utf-8″?>
 <selector xmlns:android=”http://schemas.android.com/apk/res/android”>
 <item android:state_pressed=”true”
 android:drawable=”@drawable/button_pressed” /> <!– pressed –>
 <item android:state_focused=”true”
 android:drawable=”@drawable/button_focused” /> <!– focused –>
 <item android:drawable=”@drawable/button_normal” /> <!– default –>
 </selector>将中个资源文件存放在/res/drawable 目录下,然后通过android:src 为ImageButton 指定资源。 资源定义的顺序非常重要。

本例使用Android系统自带的图像资源为三个按钮指定图像:

[html] 
<ImageButton 
 android:layout_width=”100dip” 
 android:layout_height=”50dip” 
 android:src=”@android:drawable/sym_action_call” /> 
  
<ImageButton 
 android:layout_width=”wrap_content” 
 android:layout_height=”wrap_content” 
 android:src=”@android:drawable/sym_action_chat” /> 
  
<ImageButton 
 android:layout_width=”wrap_content” 
 android:layout_height=”wrap_content” 
 android:src=”@android:drawable/sym_action_email” /> 

<ImageButton
 android:layout_width=”100dip”
 android:layout_height=”50dip”
 android:src=”@android:drawable/sym_action_call” />
 
<ImageButton
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:src=”@android:drawable/sym_action_chat” />
 
<ImageButton
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:src=”@android:drawable/sym_action_email” />

 

 \
作者:mapdigit
 

 


补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,