Android(安卓)控件开发之ImageButton
16lz
2022-07-03
/**
* 除了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,以此捕获事件并处理
* 除了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事件响应} }); }}
更多相关文章
- android EditText设置不可写
- Android(安卓)拨号器的简单实现
- android“设置”里的版本号
- android用户界面之按钮(Button)教程实例汇
- Android开发环境搭建
- 在Fragment中设置控件点击方法,执行失败。
- Android(安卓)闹钟管理类的使用
- Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色
- android 设置中划线 下划线等