开发轻松实现带文字的图片按钮

开发轻松实现带文字的图片按钮

ID:34765287

大小:578.63 KB

页数:5页

时间:2019-03-10

开发轻松实现带文字的图片按钮_第1页
开发轻松实现带文字的图片按钮_第2页
开发轻松实现带文字的图片按钮_第3页
开发轻松实现带文字的图片按钮_第4页
开发轻松实现带文字的图片按钮_第5页
资源描述:

《开发轻松实现带文字的图片按钮》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、Android开发轻松实现带文字的ImageButton实际上,ImageButton是不能添加文字的。要实现带文字的ImageButton的方法很多,这里仅列举一种方法:自定义一个继承自ImageButton的类,然后Override它的onDraw(Canvascanvas)方法。具体步骤如下:1)新建一个Android工程,例如工程名:TestImageButton。怎么建工程?不用我多说了吧。2)新建一个MyImageButton类,继承android.widget.ImageButton3)为类MyImageButton添加成员函数,详细代码如下:packagetest.swj

2、tu.edu;importandroid.content.Context;importandroid.graphics.Canvas;5importandroid.graphics.Paint;importandroid.util.AttributeSet;importandroid.widget.ImageButton;publicclassMyImageButtonextendsImageButton{publicStringtext=null;//要显示的文字publicfloattextX,textY;//文本显示的坐标位置publicintcolor;//文字的颜色public

3、MyImageButton(Contextcontext,AttributeSetattrs){super(context,attrs);textX=20;textY=60;}//设置需要显示的文本publicvoidsetText(Stringtext){this.text=text;//设置文字}//设置文本显示的颜色publicvoidsetColor(intcolor){this.color=color;//设置文字颜色}//设置显示文本的X、Y坐标publicvoidsetPosition(floatXX,floatYY){textX=XX;textY=YY;}@Overrid

4、eprotectedvoidonDraw(Canvascanvas){//TODOAuto-generatedmethodstubsuper.onDraw(canvas);Paintpaint=newPaint();paint.setTextAlign(Paint.Align.CENTER);paint.setColor(color);canvas.drawText(text,textX,textY,paint);//绘制文字}}4)在布局文件中引用:5

5、://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:background="#9CFFC1">

6、geButtonandroid:id="@+id/button01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="25dp"android:background="@drawable/video1"/>5)在启动Activity的onCreate方法中添加文字packagetest.swjtu.edu;importtest.swjtu.edu.MyImageButton;importandroid.app.Ac

7、tivity;importandroid.graphics.Color;importandroid.os.Bundle;publicclassTestImageButtonActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/privateMyImageButtonbutton01=null;@OverridepublicvoidonCreate(Bundl

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。