Android实现自定义带文字和图片的Button

  在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法。

一.用系统自带的Button实现

  最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最小。在Button的属性中有一个是drawableLeft,这个属性可以把图片设置在文字的左边,但是这种方式必须让icon的背景色是透明的,如果icon的背景色不是透明的话,会导致点击按钮时icon部分的背景色不会发生变化。

主要代码:

<Button
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">
</Button>

  实现效果:

  

  如果要让文字在图标下方,改成drawableTop即可。

二.继承系统的Button然后进行重绘

package com.test;

import android.content.Context;
import android.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,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.decodeResource(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.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="http://schemas.android.com/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> <TextView
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.test;

import android.content.Context;
import android.util.AttributeSet;
import android.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);
} 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() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "bt1被点击了", Toast.LENGTH_SHORT).show();
}
});

  三种不同方法最后的运行效果:

  工程源码下载地址:http://files.cnblogs.com/dolphin0520/TestImgTextButton.rar

Android实现自定义带文字和图片的Button的更多相关文章

  1. 【Android】Android实现自定义带文字和图片的Button

    在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最 ...

  2. Android开发学习之路-带文字的图片分享

    有用过微信分享SDK的都应该知道,微信分享到朋友圈的时候是不能同时分享图片和文字的,只要有缩略图,那么文字就不会生效.那么问题就来了,如果我们想把APP内的某些内容连带图片一起分享到微信,是不是没办法 ...

  3. Android之自定义画图文字动画

    结构: BaseView: package com.caiduping.canvas; import android.content.Context; import android.graphics. ...

  4. Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博

    原文:Android 调用系统分享文字.图片.文件,可直达微信.朋友圈.QQ.QQ空间.微博 兼容SDK 18以上的系统,直接调用系统分享功能,分享文本.图片.文件到第三方APP,如:微信.QQ.微博 ...

  5. Android 带文字的图片分享

    这里也记录下上下文,因为做了一个失物招领的App,当有人上交了失物之后,可以将这个消息分享出去,这个消息内容有物品的信息和图片,而微信SDK始终无法做到,就想着把物品信息嵌入到图片中分享出去,先放一个 ...

  6. android checkbox自定义(文字位置、格式等)

    第一种:在原生中只调整显示位置等: <CheckBox android:id="@+id/checkBox8" android:layout_width="wrap ...

  7. qml自定义带文字的button tabbutton

    https://blog.csdn.net/u014416260/article/details/54579480

  8. Android零基础入门第19节:Button使用详解

    原文:Android零基础入门第19节:Button使用详解 Button(按钮)是Android开发中使用非常频繁的组件,主要是在UI界面上生成一个按钮,该按钮可以供用户单击,当用户单击按钮时,按钮 ...

  9. Android应用程序之间共享文字和图片(一)

    以下为TestReceiveShare1工程 MainActivity如下: package cn.testreceiveshare1; import java.util.ArrayList; imp ...

随机推荐

  1. Two-Pointer 之 Run Length Coding (RLC)

    游程编码(Run Length Coding, RLC)是串处理中常见的预处理方法.其写法是典型的双指针(Two-Pointer).下面总结其写法1.输入为一串整数可以不把整数存在数组里

  2. Flash调用麦克风

    import flash.events.ActivityEvent;import flash.media.Microphone;var deviceArray:Array = Microphone.n ...

  3. mysql索引失效

    在做项目的过程中,难免会遇到明明给mysql建立了索引,可是查询还是很缓慢的情况出现,下面我们来具体分析下这种情况出现的原因及解决方法   索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: ...

  4. JavaScript中getBoundingClientRect()方法详解

    获取浏览器滚动的高度: scrollTop=document.documentElement.scrollTop || document.body.scrollTop getBoundingClien ...

  5. 如何优化用SQL语句INSERT INTO … SELECT插入数据时锁全表的问题

    1.binlog format 启用Row Based Replication(行复制)模式: SET GLOBAL binlog_format = 'ROW'; 如果你想永久的启用这个模式,请修改m ...

  6. 创建你的第一个JavaScript库

    是否曾对Mootools的魔力感到惊奇?是否有想知道Dojo如何做到那样的?是否对jQuery感到好奇?在这个教程中,我们将了解它们背后的东西并且动手创建一个超级简单的你最喜欢的库. 我们其乎每天都在 ...

  7. Silicon C8051F340之时钟系统

    一.背景 做个记录,以备下次快速开发. 二.正文 C8051F340有一个可编程内部高频振荡器.一个可编程内部低频振荡器.一个外部振荡器驱动电路 和一个4倍时钟乘法器.其中可编程内部高频振荡器在系统复 ...

  8. vue2.0学习(一)

    1.解决双花括号在初始化时的闪烁,两种方式,一种是<div v-text="name"></div>,将用v-text指令来显示,类似于angular的ng ...

  9. OpenCV 2.4.13 编译使用(VS2015下)

    OpenCV2.4.13编译(VS2015) 这里给出已经编译好的的下载路径.包括Win64的debug和release版本. OpenCV for MSVC14 Win64 1.下载OpenCV源码 ...

  10. wampServer图标为橙色无法启动原因之一

    前段时间,自己在本地做了一个WordPress的网站,利用wampserver配置的,后来突然无法启动了. 经过仔细查找发现是因为之前装了sql server,导致wampServer无法启动,那么怎 ...