/**
* 除了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()        {@Overridepublic void onClick(View arg0) {//ImageButton事件响应}                 });    }}


更多相关文章

  1. android EditText设置不可写
  2. Android(安卓)拨号器的简单实现
  3. android“设置”里的版本号
  4. android用户界面之按钮(Button)教程实例汇
  5. Android开发环境搭建
  6. 在Fragment中设置控件点击方法,执行失败。
  7. Android(安卓)闹钟管理类的使用
  8. Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色
  9. android 设置中划线 下划线等

随机推荐

  1. 前言:Android进阶汇总(持续更新)
  2. 【Android】入门级连接网络示例: 网页浏览
  3. Android 学习记录-ImageView显示格式
  4. Android虚拟机大屏幕设置
  5. android - JNI NewStringUTF字串的釋放
  6. 全网最全的Android资源汇总
  7. Android中后台显示悬浮窗口的方法
  8. Android(安卓)Fragment 你应该知道的一切
  9. 系出名门Android(4) - 活动(Activity),
  10. 更新Android SDK Tools, revision 7报错