android开发怎样android 键盘添加按钮钮

                    Android实现自定义带文字和图片的Button
  在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法。
一.用系统自带的Button实现
  最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最小。在Button的属性中有一个是drawableLeft,这个属性可以把图片设置在文字的左边,但是这种方式必须让icon的背景色是透明的,如果icon的背景色不是透明的话,会导致点击按钮时icon部分的背景色不会发生变化。
主要代码:
android:id="@+id/bt3"
android:layout_marginTop="4dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="火车"
android:textSize="16sp"
android:textColor="#000000"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:drawableLeft="@drawable/line_bus_icon"
android:background="@drawable/button_bg"&
  实现效果:
  如果要让文字在图标下方,改成drawableTop即可。&
二.继承系统的Button然后进行重绘
package com.
import android.content.C
import android.graphics.B
import android.graphics.BitmapF
import android.graphics.C
import android.util.AttributeS
import android.widget.B
public class ImageTextButton2 extends Button {
private int resourceId = 0;
public ImageTextButton2(Context context) {
super(context,null);
public ImageTextButton2(Context context,AttributeSet attributeSet) {
super(context, attributeSet);
this.setClickable(true);
resourceId = R.drawable.
bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
public void setIcon(int resourceId)
this.bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
invalidate();
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// 图片顶部居中显示
int x = (this.getMeasuredWidth() - bitmap.getWidth())/2;
int y = 0;
canvas.drawBitmap(bitmap, x, y, null);
// 坐标需要转换,因为默认情况下Button中的文字居中显示
// 这里需要让文字在底部显示
canvas.translate(0,(this.getMeasuredHeight()/2) - (int) this.getTextSize());
super.onDraw(canvas);
  然后再布局文件中调用:
&com.test.ImageTextButton2
android:id="@+id/bt2"
android:layout_marginTop="10dp"
android:text="hello"
android:textSize="15dp"
android:textColor="#000000"
android:layout_width="60dp"
android:layout_height="70dp"
android:background="@drawable/button_bg"
  注意,在xml文件中调用时,对于layout_width和layout_height两个属性千万不能用wrap_content,否则会导致按钮显示出来的只有文字部分。
三.继承布局文件
  分析发现一个带文字和icon的button其实可以看成一个线性布局或相对布局,因此可以继承布局来实现。
  先实现一个button的布局文件img_text_bt.xml:
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout
xmlns:android="/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"&
&ImageView
android:id="@+id/imgview"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/icon"&
&/ImageView&
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@id/imgview"&
&/TextView&
&/RelativeLayout&
  然后去继承RelativeLayout布局:
package com.
import android.content.C
import android.util.AttributeS
import android.view.LayoutI
import android.widget.ImageV
import android.widget.RelativeL
import android.widget.TextV
public class ImageTextButton1 extends RelativeLayout {
private ImageView imgV
private TextView
public ImageTextButton1(Context context) {
super(context,null);
public ImageTextButton1(Context context,AttributeSet attributeSet) {
super(context, attributeSet);
LayoutInflater.from(context).inflate(R.layout.img_text_bt, this,true);
this.imgView = (ImageView)findViewById(R.id.imgview);
this.textView = (TextView)findViewById(R.id.textview);
this.setClickable(true);
this.setFocusable(true);
public void setImgResource(int resourceID) {
this.imgView.setImageResource(resourceID);
public void setText(String text) {
this.textView.setText(text);
public void setTextColor(int color) {
this.textView.setTextColor(color);
public void setTextSize(float size) {
this.textView.setTextSize(size);
  然后就可以在需要的xml文件中调用:
&com.test.ImageTextButton1
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_bg"
  再在Activity中使用:
     bt1 = (ImageTextButton1)findViewById(R.id.bt1);
bt1.setText("icon");
bt1.setTextColor(Color.rgb(0, 0, 0));
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "bt1被点击了", Toast.LENGTH_SHORT).show();
  三种不同方法最后的运行效果:
  工程源码下载地址:
阅读(...) 评论()博客分类:
五、基本界面控件-3图片控件
5.3 图片控件
5.3.1 ImageView
图5.3.1ImageView
android.widget.ImageView图片控件,继承自android.view.View,在android.widget包中。
最简单的使用方法。src设置图片路径,可引用drawable的图片。
&ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tool"/&
动态声明ImageView,设置src。
ImageView imageView = new ImageView(this);
InputStream inputStream = super.getAssets().open("home.png");
imageView.setImageDrawable(Drawable.createFromStream(inputStream, "tackpic"));
this.imageLayout.addView(imageView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
} catch (IOException e) {
e.printStackTrace();
5.3.2 ImageButton
图5.3.2ImageButton
android.widget.ImageButton图片控件,继承自android.widget.ImageView,在android.widget包中。
最简单的使用方法。src设置图片路径,可引用drawable的图片。
&ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/but_01"/&
动态声明ImageView,设置src。
ImageButton imageButton = new ImageButton(this);
InputStream inputStream = super.getAssets().open("but_02.png");
imageButton.setImageDrawable(Drawable.createFromStream(inputStream, "tackpic"));
this.imageLayout.addView(imageButton, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
} catch (IOException e) {
e.printStackTrace();
5.3.3 ImageSwitcher和Gallery
图5.3.3 ImageSwitcher
android.widget. ImageSwitcher图片控件,继承自android.widget.ViewSwitcher(ViewGroup)。在android.widget包中。
ImageSwithcer是用来图片显示那块区域的控件,使用方法setInAnimation(Animation),setOutAnimation(Animation)设置动画。
Gallery是来控制底下那个图标索引列表索引用的。ImageAdapter继承自BaseAdapter,设置Gallery的适配器。
在layout添加ImageSwitcher和Gallery。定义Activity,implements接口OnItemSelectedListener, ViewFactory。onCreate的时候定义要显示图片路径列表,设置Gallery的Adapter。onItemSelected事件触发时,设置对应的图片。
Layout文件。
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&ImageSwitcher android:id="@+id/switcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" /&
&Gallery android:id="@+id/gallery"
android:background="#"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp" /&
&/RelativeLayout&
SwitcherActivity类。
public class SwitcherActivity extends Activity implements OnItemSelectedListener, ViewFactory {
private ImageSwitcher imageS
private ArrayList&String& imageAssetPathList = new ArrayList&String&();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.switcher);
this.imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
this.gallery = (Gallery) findViewById(R.id.gallery);
for (int i = 1; i &= 20; i++) {
this.imageAssetPathList.add("images/" + i + ".jpg");
this.imageSwitcher.setFactory(this);
this.gallery.setAdapter(new ImageAdapter(this, this.imageAssetPathList));
this.gallery.setOnItemSelectedListener(this);
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageV
public void onItemSelected(AdapterView&?& parent, View view, int position, long id) {
InputStream inputStream = super.getAssets().open(this.imageAssetPathList.get(position));
imageSwitcher.setImageDrawable(Drawable.createFromStream(inputStream, "" + position));
} catch (IOException e) {
e.printStackTrace();
public void onNothingSelected(AdapterView&?& parent) {
ImageAdapter类
public class ImageAdapter extends BaseAdapter {
private ArrayList&String& imageAssetPathL
public ImageAdapter(Context content, ArrayList&String& imageAssetPathList) {
this.content =
this.imageAssetPathList = imageAssetPathL
public int getCount() {
if (this.imageAssetPathList != null) {
return this.imageAssetPathList.size();
public Object getItem(int position) {
public long getItemId(int position) {
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageV
imageView = new ImageView(this.content);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(0, 0, 0, 0);
InputStream inputStream = this.content.getAssets().open(this.imageAssetPathList.get(position));
imageView.setImageDrawable(Drawable.createFromStream(inputStream, "" + position));
return imageV
} catch (IOException e) {
e.printStackTrace();
浏览 14102
limingnihao
浏览: 1591889 次
来自: 北京
好详细,写的真全面
哪里有源代码啊,?能否发一份?还有就是 ClassEntity ...
classentity是啥?哪个包的类啊?这教程并不完整啊!
shift+alt+a
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'I want to create a custom layout for my google plus button, any ideas? I've tried calling the OnClickEvent of the google plus button (that doesn't work) and I've tried changing the background image. Source code:
&com.google.android.gms.plus.PlusOneButton
xmlns:plus="/apk/lib/com.google.android.gms.plus"
android:id="@+id/plus_one_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
plus:size="standard"
plus:annotation="inline"/&
holder.mPlusOneButton = (PlusOneButton) holder.content.findViewById(R.id.plus_one_button);
holder.mPlusOneButton.initialize("http://www.xxx.xx/", 0);
Add a custom button to your layout
Set OnClickListener to your custom button
Use the PlusClient as described
to handle the login procedure
As example I can provide my controller class for handling Google Plus login:
public class GooglePlusLoginController implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
public static final int REQUEST_CODE_SIGN_IN = 100;
private PlusClient googlePlusC
private ConnectionResult connectionR
public GooglePlusLoginController(Activity activity) {
this.activity =
googlePlusClient = new PlusClient.Builder(activity, this, this)
.setActions("/AddActivity")
.setScopes(Scopes.PLUS_LOGIN) // Space separated list of scopes
googlePlusClient.connect();
// call this method in your click handler
public void login() {
connectionResult.startResolutionForResult(activity, REQUEST_CODE_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
googlePlusClient.connect();
// call this method in your activity's onActivityResult
public void onActivityResult() {
if(!googlePlusClient.isConnected() && !googlePlusClient.isConnecting()) {
googlePlusClient.connect();
public void onConnected(Bundle bundle) {
// connected, you can now get user's data from
// googlePlusClient.getCurrentPerson()
public void onDisconnected() {
public void onConnectionFailed(ConnectionResult result) {
connectionResult =
private void logout() {
if(googlePlusClient.isConnected()) {
googlePlusClient.clearDefaultAccount();
googlePlusClient.disconnect();
googlePlusClient.connect();
本文地址: &
我要创造我的谷歌加号按钮,任何想法自定义布局?我试过致电谷歌加号按钮的OnClickEvent(即不工作),我已经试图改变的背景图像。来源$ C $ C: < com.google.android.gms.plus.PlusOneButton
的xmlns:加=“/apk/lib/com.google.android.gms.plus”
机器人:ID =“@ + ID / plus_one_button”
机器人:layout_width =“WRAP_CONTENT”
机器人:layout_height =“WRAP_CONTENT”
加:大小=“标准”
加:注释=“内联”/>
holder.mPlusOneButton =(PlusOneButton)holder.content.findViewById(R.id.plus_one_button);
holder.mPlusOneButton.initialize(“HTTP://www.xxx.xx/”,0); 解决方案 添加自定义按钮布局设置 OnClickListener 您的自定义按钮使用 PlusClient 描述这里处理登录程序作为例子,我可以提供我的控制器类来处理谷歌加登录: 公共类GooglePlusLoginController实现GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener {
公共静态最终诠释REQUEST_ code_SIGN_IN = 100;
私人PlusClient googlePlusC
私人ConnectionResult connectionR
私人活动活动;
公共GooglePlusLoginController(活动活动){
this.activity =活动;
googlePlusClient =新PlusClient.Builder(活动,这,这)
.setActions(“/AddActivity”)
.setScopes(Scopes.PLUS_LOGIN)//空格分隔范围清单
。建立();
googlePlusClient.connect();
//调用此方法在点击处理程序
公共无效登录(){
connectionResult.startResolutionForResult(活动,REQUEST_ code_SIGN_IN);
}赶上(IntentSender.SendIntentException E){
googlePlusClient.connect();
//调用此方法在您的活动的的onActivityResult
公共无效的onActivityResult(){
如果(googlePlusClient.isConnected()及!&放大器;!googlePlusClient.isConnecting()){
googlePlusClient.connect();
公共无效onConnected(束束){
//连接,你现在可以从用户的数据
// googlePlusClient.getCurrentPerson()
公共无效onDisconnected(){
公共无效onConnectionFailed(ConnectionResult结果){
connectionResult =结果;
私人无效注销(){
如果(googlePlusClient.isConnected()){
googlePlusClient.clearDefaultAccount();
googlePlusClient.disconnect();
googlePlusClient.connect();
本文地址: &
扫一扫关注官方微信 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
[android开发学习30]动态创建多个按钮,并给每个按键添加监听事件
下载积分:1000
内容提示:[android开发学习30]动态创建多个按钮,并给每个按键添加监听事件
文档格式:PDF|
浏览次数:90|
上传日期: 02:37:44|
文档星级:
全文阅读已结束,如果下载本文需要使用
 1000 积分
下载此文档
该用户还上传了这些文档
[android开发学习30]动态创建多个按钮,并给每个按键添
关注微信公众号[Android开发学习30]动态创建多个按钮,并给每个按键添加监听事件
//获取屏幕大小,以合理设定 按钮 大小及位置
& DisplayMetrics dm = new DisplayMetrics();
& getWindowManager().getDefaultDisplay().getMetrics(dm);
& int width = dm.widthP
& int height = dm.heightP
&//自定义layout
& RelativeLayout layout = new RelativeLayout(this);&&&&&&&&
&//这里创建16个按钮,每行放置4个按钮
& Button Btn[] = new Button[16];
& int j = -1;
& for& (int i=0; i&=15; i++) {&&&&&
&&&&&&& Btn[i]=new Button(this);
&&&&&&& Btn[i].setId(2000+i);
&&&&&&& Btn[i].setText(&按钮&+i);&&
&&&&&&& RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams ((width-50)/4,40);& //设
置按钮的宽度和高度
&&&&&&& if (i%4 == 0) {
&&&&&&&& j++;
&&&&&&& btParams.leftMargin = 10+ ((width-50)/4+10)*(i%4);&& //横坐标定位&&&&&&&
&&&&&&& btParams.topMargin = 20 + 55*j;&& //纵坐标定位&&&&&&
&&&&&&& layout.addView(Btn[i],btParams);&& //将按钮放入layout组件
&&&& this.setContentView(layout);
&&& //批量设置监听
& for (int k = 0; k &= Btn.length-1; k++) {
&& //这里不需要findId,因为创建的时候已经确定哪个按钮对应哪个Id
&& Btn[k].setTag(k);&&&&&&&&&&&&&&& //为按钮设置一个标记,来确认是按下了哪一个按钮
&& Btn[k].setOnClickListener(new Button.OnClickListener() {
&&& @Override
&&&&&&& public void onClick(View v) {
&&&&&&&&&&& int i = (Integer) v.getTag();&& //这里的i不能在外部定义,因为内部类的关系,内部类好多繁琐的
东西,要好好研究一番
&&&&&&&&&&& Intent intent = new Intent();
&&&&&&&&&&& intent.setClass(Work_01.this, Work_02.class);
&&&&&&&&&&& Bundle bundle = new Bundle();
&&&&&&&&&&& bundle.putInt(&count&, i);
&&&&&&&&&&& intent.putExtras(bundle);
&&&&&&&&&&& startActivity(intent);
&&&&&&&&&&& Work_01.this.finish();&&&&&&&&&&&&&&&&&&
然后,可以在Work_02里将&按下的按钮为:& i 打印出来,以确认设置的监听是否正确。
比如,按下 按钮0 ,在Work_02对应的页面里,应该打印出 &按下的按钮为:0 &。}

我要回帖

更多关于 android 添加按钮事件 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信