API Demos 2.3 学习笔记 (8)-- Views->ImageButton

更多精彩内容,请点击阅读:《API Demos 2.3 学习笔记》


ImageButton示例介绍了定义ImageButton的基本方法。从示例布局文件来看,主要是在线性LinearLayout中定义三个ImageButton。不同的是,这三个按钮通过android:src属性分别引用三张来自android系统内置的图片作为按钮图标。
下面我们以ImageButton为例,简单介绍如何引用android系统内置图标资源。
1、在java代码中引用

在java代码中,我们通过“android.R.drawable.图标名称”格式来引用系统图标资源。具体参考如下:

        private ImageButton mImageButton = null;

        //通过findViewById获得ImageButton          
        mImageButton = (ImageButton)findViewById(R.id.myImageButton01);
        
        //引用android内置图标,作为ImageButton的按钮图标 
        mImageButton.setImageResource(android.R.drawable.sym_action_call);

2、在xml布局文件中

在布局文件中,我们通常按照"@android:drawable/图标名称"格式来引用资源。
具体参考如下:
<ImageButton
android:id="@+id/myImageButton01"
android:layout_width="100dip"
android:layout_height="50dip"
android:src="@android:drawable/sym_action_call"/>
注:我们可以在AndroidSDK目录下找到这些系统内置图标资源,具体位置在对应Android版本的资源目录下。以android2.3为例,这些图标在android-sdk-linux_86/platforms/android-9/data/res/drawable-hdpi目录下。

另外,除了引用android系统内置图标之外,我们也可以引用自定义图标。具体操作如下:
1、为了表示按钮不同状态(例如:被聚焦,被点击等),我们可以为每种状态定义一张图片。首先,我们准备三张类似的图片,放在drawable目录下。




button_normal

button_pressed

button_focused

2、在drawable目录下新建一个xml文件btn_star.xml,通过"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>

3、以在xml布局文件中引用为例,在引用该自定义图标时,图标名称为定义在drawable下的xml文件名称(不包括xml后缀)。例如,上面我们定义了btn_star.xml,引用时,可以这样引用:"@android:drawable/btn_star"。

<ImageButton
android:id="@+id/myImageButton04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/btn_star"/>

下面我们进行实例代码解析:

res-layout-image_button_1.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

     <!-- ImageButton,引用android系统内置图标sym_action_call(拨打电话)作为按钮图标  -->
    <ImageButton
    	android:id="@+id/myImageButton01"
    	android:layout_width="100dip"
    	android:layout_height="50dip"
    	android:src="@android:drawable/sym_action_call" />

     <!-- ImageButton,引用android系统内置图标sym_action_chat(聊天)作为按钮图标  -->
   	<ImageButton
    	android:id="@+id/myImageButton02"   	
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:src="@android:drawable/sym_action_chat" />	

     <!-- ImageButton,引用android系统内置图标sym_action_email(邮件)作为按钮图标 -->
   	<ImageButton
     	android:id="@+id/myImageButton03"   	  	
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:src="@android:drawable/sym_action_email" />	
    	
     <!-- ImageButton,引用自定义图标作为按钮图标 -->
   	<ImageButton
     	android:id="@+id/myImageButton04"   	  	
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:src="@android:drawable/btn_star" />	    	
</LinearLayout>

src-com.example.android.apis.view-ImageButton1.java

package com.example.android.apis.view;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;


import com.example.android.apis.R;


public class ImageButton1 extends Activity {
    private ImageButton mImageButton = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_button_1);
  
        //通过findViewById获得ImageButton          
        mImageButton = (ImageButton)findViewById(R.id.myImageButton01);
        
        //引用android内置图标,作为ImageButton的按钮图标 
        mImageButton.setImageResource(android.R.drawable.sym_action_call);
        
    }
}

预览效果:



欢迎大家关注我的微信公众号:

微信公众号:sn0wdr1am


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值