Android ImageButton Example 图片按钮
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 图片按钮的更多相关文章
- Android ImageButton单击切换按钮图片效果
正常状态的效果: 按钮按下的效果图片: 一.在java中为图片按钮增加触摸监听的函数来实现图片切换,代码如下: ImageButton btn = (ImageButton)findViewById( ...
- android imageButton 点击按钮前中后,按钮颜色的变化
我们在开发的过程中,往往为了美化界面的需要,会修改按钮的默认外观,而因为Android中的按钮有三种状态—默认,被点击,被选中.所以,如果要改变按钮的外观,需要对这三种情况都做出修改,也许在以往,我们 ...
- android imageButton 透明图片
在Android有许多不规则button.例如: 这个时候,我们假设想做成不规则button的话.第一步就是搞一张边缘透明的png图片,然后用src指定到他.这个时候我们会发现,还没有达到要的效果.还 ...
- 图片按钮(imageButton)
图片按钮(imageButton) 常用属性: android:src="@drawable/download" (这里的download是一张图片的名称,通过引用该图片的名称直接 ...
- android带有文字的图片按钮的两种实现方式
android带有文字的图片按钮的两种实现方式 1). TextView对Button用相对布局,这要要求按钮的背景图片要留下空白位置给文字.这种方式开发比较简单,适合做一些风格一致的Button. ...
- Android ImageButton的使用。
1.首先是范例代码,一个基本的ImageButton响应. package com.example.arlxsdemo; import android.graphics.Bitmap; import ...
- [CSS]Input标签与图片按钮对齐
页面直接摆放一个input文本框与ImageButton图片按钮,但是发现没有对齐: <input type="text" id="txtQty" /&g ...
- 解决android:background背景图片被拉伸问题
ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸.src是图片内容(前 ...
- WPF利用Image实现图片按钮
之前有一篇文章也是采用了Image实现的图片按钮,不过时间太久远了,忘记了地址.好吧,这里我进行了进一步的改进,原来的文章中需要设置4张图片,分别为可用时,鼠标悬浮时,按钮按下时,按钮不可用时的图片, ...
随机推荐
- 屏幕编程 F4的帮组用法
PROCESS ON VALUE-REQUEST. * 设置帮助(工作中心) FIELD wa_zppt026-arbpl MODULE mdl_arbpl_f4. *&---------- ...
- ocx控件避免弹出警告的类--2
本文与 OCX控件避免弹出安全警告的类 http://www.cnblogs.com/lidabo/archive/2013/03/26/2981852.html 有些类似,只不过增加了几行代码(红色 ...
- Swift - 分段选择控件(UISegmentedControl)的用法
1,选择控件的创建,并监听控件选择值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class ViewController: UIVie ...
- Windbg 32位版本和64位版本的选择
习惯了Vsiual Studio的兄弟们可能会因为先入为主的原因以为所有的调试器都应该像它那样,其实不然,当你安装Debugging Tools for Windows的时候,你将发现有两个系列的工具 ...
- Common Lisp第三方库介绍 | (R "think-of-lisper" 'Albertlee)
Common Lisp第三方库介绍 | (R "think-of-lisper" 'Albertlee) Common Lisp第三方库介绍 一个丰富且高质量的开发库集合,对于实际 ...
- Codechef Not a Triangle题解
找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...
- 如何制作python安装模块(setup.py)
Python模块的安装方法: 1. 单文件模块:直接把文件拷贝到$python_dir/lib 2. 多文件模块,带setup.py:python setup.py install 3. egg文件, ...
- 使用函数指针和多态代替冗长的if-else或者switch-case
在编程中,if-else和switch-case是很常见的分支结构,很少在程序中不用这些控制语句.但是不能否认,在一些场景下,由于分支结构过分长,导致代码不美观且不容易维护,在<重构>一书 ...
- perl 访问网站一些useragent的设置
121.40.205.143 - - [22/Jun/2016:12:56:23 +0800] "GET /wechat/account.html HTTP/1.1" 200 34 ...
- PHP操作Mysql中间BLOB场
1.MySQL在BLOB字段类型 BLOB场的类型用于存储二进制数据. MySQL在.BLOB它是一种类型的一系列.含有:TinyBlob.Blob.MediumBlob.LongBlob.大小上不同 ...