Android 控件开发之ImageButton

/**
 * 除了Android系统自带的Button按钮一万,还提供了带图标的按钮ImageButton 
 * 要制作带图标的按钮,首先要在布局文件中定义ImageButton,然后通过setImageDrawable
 * 方法来设置要显示的图标。

注意:
我们可以在布局文件中就直接设置按钮的图标,如
android:src="@drawable/icon1"

我们也可以在程序中设置自定义图标
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));

我们还可以使用系统自带的图标
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));

       设置完按钮的图标后,需要为按钮设置监听setOnClickListener,以此捕获事件并处理

*/

   ImageButton 效果图:



本程序main.xml源码:
  

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="这是一个带图片的按钮.ImageButton"/>
    
<ImageButton 
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>





本程序java源码

package com.sx.ImageButton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;


public class ImageButtonActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton);
        
        imageButton.setImageDrawable((getResources().getDrawable(R.drawable.icon)));
        
        imageButton.setOnClickListener(new Button.OnClickListener()
        {
		@Override
		public void onClick(View arg0) 
		{
			//ImageButton事件响应
		}
         
        });
    }
}



   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值