Android ImageButton Example 图片按钮

使用“android.widget.ImageButton” 展现一个具有背景图片的按钮

本教程将展现一个具有名字为 c.png背景图片的按钮。当用户点击该图片按钮,弹出提示信息。

1. 添加 .png 图片资源:
将 “android_button.png” 文件,放到 res/drawable-hdpi 目录下

2. 添加 ImageButton 标记
  打开 “res/layout/activity_main.xm” 文件, 添加 “ImageButton” 标记。

通过 “android:src“ 引用图片资源

File : res/layout/activity_main.xm

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android_button"
android:contentDescription="这是一个图片Button">
</ImageButton> </LinearLayout>

3. Code Code

add a click listener on image button.

File : MainActivity.java

package com.jiangge.imagebuttondemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast; public class MainActivity extends Activity { ImageButton imageButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); addListenerOnImageButton();
} private void addListenerOnImageButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "ImageButton 被点击啦", Toast.LENGTH_SHORT).show();
}
});
}
}

4、运行结果:

点击图片按钮:

参考文献:

http://www.mkyong.com/android/android-imagebutton-example/

Android ImageButton Example 图片按钮的更多相关文章

  1. Android ImageButton单击切换按钮图片效果

    正常状态的效果: 按钮按下的效果图片: 一.在java中为图片按钮增加触摸监听的函数来实现图片切换,代码如下: ImageButton btn = (ImageButton)findViewById( ...

  2. android imageButton 点击按钮前中后,按钮颜色的变化

    我们在开发的过程中,往往为了美化界面的需要,会修改按钮的默认外观,而因为Android中的按钮有三种状态—默认,被点击,被选中.所以,如果要改变按钮的外观,需要对这三种情况都做出修改,也许在以往,我们 ...

  3. android imageButton 透明图片

    在Android有许多不规则button.例如: 这个时候,我们假设想做成不规则button的话.第一步就是搞一张边缘透明的png图片,然后用src指定到他.这个时候我们会发现,还没有达到要的效果.还 ...

  4. 图片按钮(imageButton)

    图片按钮(imageButton) 常用属性: android:src="@drawable/download" (这里的download是一张图片的名称,通过引用该图片的名称直接 ...

  5. android带有文字的图片按钮的两种实现方式

    android带有文字的图片按钮的两种实现方式 1). TextView对Button用相对布局,这要要求按钮的背景图片要留下空白位置给文字.这种方式开发比较简单,适合做一些风格一致的Button. ...

  6. Android ImageButton的使用。

    1.首先是范例代码,一个基本的ImageButton响应. package com.example.arlxsdemo; import android.graphics.Bitmap; import ...

  7. [CSS]Input标签与图片按钮对齐

    页面直接摆放一个input文本框与ImageButton图片按钮,但是发现没有对齐: <input type="text" id="txtQty" /&g ...

  8. 解决android:background背景图片被拉伸问题

    ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸.src是图片内容(前 ...

  9. WPF利用Image实现图片按钮

    之前有一篇文章也是采用了Image实现的图片按钮,不过时间太久远了,忘记了地址.好吧,这里我进行了进一步的改进,原来的文章中需要设置4张图片,分别为可用时,鼠标悬浮时,按钮按下时,按钮不可用时的图片, ...

随机推荐

  1. 新秀操作和维护注意事项:Windows关于使用Xshell管理你的云主机

    假设你PC它是linux系统.那么直接与终端ssh命令就可以了.假设Windows系统.使用它是必要的sshclient. PS:我双系统. 有时候,他们想使用Windows的. Windows上ss ...

  2. linux公社的大了免费在线android资料

    2011年linux数据库的android在线分享 linux公社:开源公社             本文撰写:杨凯专属频道 2011年9月12日 21:39 <目录> Android 3 ...

  3. RTSP 消息拼装实例代码

    整理了下之前文章提到的RTSP实例的代码,主要是拼装各类消息字段. 首先,抽取一个公共函数,用来根据消息类型,生成不同的format,供sprintf试用. char * GetRTSPCmd( co ...

  4. 【Visual C++】绘图函数BitBlt的使用方法

    BitBlt 该函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境. 原型: BOOL BitBlt( HDC  hdcDest, int  nXDest,   ...

  5. 14.2.5.7 Physical Row Structure 物理数据结构:

    14.2.5.7 Physical Row Structure 物理数据结构: InnoDB物理记录结构依赖行格式 在表创建的时候, 默认, InnoDB 使用Antelope 文件存储格式和它的压缩 ...

  6. javascript实现的可改变滚动方向的无缝滚动

    效果图如下: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  7. Php 解析XML文件

    Php 解析XML文件 Php 解析XML文件,仅供学习參考!演示样例代码例如以下: <?php header("Content-type: text/html; charset=ut ...

  8. virus.win32.parite.H病毒的查杀方法

    virus.win32.parite.H病毒的查杀方法 昨天电脑中了virus.win32.parite.H病毒,搞了2个多小时最终搞定了.以下记录下我的解决方法. 第一步:下载Win32.Parit ...

  9. 使用Python在2M内存中排序一百万个32位整数

    译言网 | 使用Python在2M内存中排序一百万个32位整数 使用Python在2M内存中排序一百万个32位整数 译者:小鼠 发表时间:2008-11-13浏览量:6757评论数:2挑错数:0 作者 ...

  10. 从零开始学C++之从C到C++(二):引用、内联函数inline、四种类型转换运算符

    一.引用 (1).引用是给一个变量起别名 定义引用的一般格式:类型  &引用名 = 变量名: 例如:int a=1; int  &b=a;// b是a的别名,因此a和b是同一个单元 注 ...