版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Android实现自定义带文字和图片的Button在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法。一.用系统自带的Button实现最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最小。在Button的属性中有一个是drawableLeft,这个属性可以把图片设置在文字的左边,但是这种方式必须让icon的背景色是透明的,如果icon的背景色不是透明的话,会导致点击按钮时icon部分的背景色不会发生变化。主要代码:<Button android:id="+id/bt3" android:layout_mar
2、ginTop="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:paddi
3、ngRight="5dp" android:paddingBottom="5dp" android:drawableLeft="drawable/line_bus_icon" android:background="drawable/button_bg"> </Button>实现效果:如果要让文字在图标下方,改成drawableTop即可。 二.继承系统的Button然后进行重绘package com.test;import android.content.Context;import andro
4、id.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.util.AttributeSet;import android.widget.Button;public class ImageTextButton2 extends Button private int resourceId = 0; private Bitmap bitmap; public ImageTextButton2(Context context) super(context
5、,null); public ImageTextButton2(Context context,AttributeSet attributeSet) super(context, attributeSet); this.setClickable(true); resourceId = R.drawable.icon; bitmap = BitmapFactory.decodeResource(getResources(), resourceId); public void setIcon(int resourceId) this.bitmap = BitmapFactory.decodeRes
6、ource(getResources(), resourceId); invalidate(); Override 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.t
7、ranslate(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
8、" android:layout_width="60dp" android:layout_height="70dp" android:background="drawable/button_bg" />注意,在xml文件中调用时,对于layout_width和layout_height两个属性千万不能用wrap_content,否则会导致按钮显示出来的只有文字部分。三.继承布局文件分析发现一个带文字和icon的button其实可以看成一个线性布局或相对布局,因此可以继承布局来实现。先实现一个button的布局文件img
9、_text_bt.xml:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="+id/imgview" android:layout_alignParentTop="t
10、rue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="drawable/icon"> </ImageView> <TextView android:id="+id/textview" android:layout_width="wrap_content&
11、quot; android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_below="id/imgview"> </TextView> </RelativeLayout>然后去继承RelativeLayout布局:package com.test;import android.content.Context;import android.util.AttributeSet;import a
12、ndroid.view.LayoutInflater;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;public class ImageTextButton1 extends RelativeLayout private ImageView imgView; private TextView textView; public ImageTextButton1(Context context) super(context,null); publ
13、ic 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); th
14、is.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.setTextS
15、ize(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.setTextCo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026安徽蚌埠五河县乡村医生招聘16人笔试备考题库及答案解析
- 2026年贵州工程职业学院单招综合素质笔试备考题库含详细答案解析
- 2026广东深圳宝安区航城街道中心幼儿园招聘笔试备考题库及答案解析
- 2026上半年云南事业单位联考红河州招聘1570人考试重点题库及答案解析
- 2026河北石家庄市元氏县人力资源和社会保障局从2022年三支一扶志愿者中专项招聘事业单位工作人员3人笔试备考题库及答案解析
- 2026年闽江师范高等专科学校高职单招职业适应性测试备考题库及答案详细解析
- 2026安徽合肥市五十中学西校教育集团心湖校区招聘教师笔试备考题库及答案解析
- 2026江西吉安市吉水县金滩新区开发建设有限公司面向社会招聘1名食堂经理笔试备考试题及答案解析
- 2026上半年陕西事业单位联考商洛市招聘307人笔试备考题库及答案解析
- 2026浙江绍兴市外服人力资源服务有限公司聘用制人员招聘1人笔试备考题库及答案解析
- 常州市2025-2026学年第一学期高三期末质量调研政治+答案
- 签字版离婚协议书范本
- 《神笔马良》:儿童读书分享
- 外研版五年级英语下册单词表
- 《木兰诗》历年中考古诗欣赏试题汇编(截至2024年)
- 技能认证催化高级考试(习题卷28)
- 拖欠工程款民事判决书
- 2011~2020年上海市高考语文文言文真题译文全集
- 设立年产20万吨固体氢氧化钠项目安全评估报告
- 主斜井管路安装
评论
0/150
提交评论