在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】Android实现自定义带文字和图片的Button的更多相关文章

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

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

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

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

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

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

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

    MainActivity如下: package cn.testshare1; import java.io.File; import java.util.ArrayList; import andro ...

  5. Android 带文字的图片分享

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

  6. android控件---自定义带文本的ImageButton

    由于SDK提供的ImageButton只能添加图片,不能添加文字:而Button控件添加的文字只能显示在图片内部:当我们需要添加文字在图片外部时就不能满足我们的需求了,顾只能自己写个自定义ImageB ...

  7. 【Android UI】自定义带按钮的标题栏

    自定义标题栏在很多的android app中很常见,可以说是一种很有用的UI设计方法.自 己也本着学习的态度,经过一番各种坑,终于实现了,现总结如下: 一:大致流程 1.      对指定的andro ...

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

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

  9. [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)

    http://blog.csdn.net/yanzi1225627/article/details/22439119 众所周知,想要让ImageView旋转的话,可以用setRotation()让其围 ...

随机推荐

  1. 判断list为空的条件

    if(list!=null&&!list.isEmpty()){                    hql.append(" and (status = ? or sta ...

  2. Node.js连接postgres

    一.下载Node.js postgres驱动 Node.js里面没有postgres模块的,我们需要安装node-postgres模块. node-postgres模块的下载地址为:https://g ...

  3. 竟然没有转载。。。A Few of My Favorite HTML5 and CSS3 Online Tools

    HTML5 Boilerplate HTML5 Boilerplate provides a great way to get started building HTML5 sites. It inc ...

  4. 关于在Andoird集成开发软件中添加外部jar包的方法

    步骤必须是下面的两步,少一步都不行. 第一步是存放于项目中,第二步是导入和应用于项目中. 1.右键项目-Build Path-Configure Build Path-在Libraries目录下-点右 ...

  5. bt开源的客户端——xbt client

    我部署好了bt tracker, 用bitcomet可以下载. 但xbt client下载不来.torrent资源.

  6. 今天搞log4net插入错误日志去mysql数据库的时候出现了点问题,已解决。记录下解决方案

    先上图 配置log4net的时候要填这项,可是这个value我不知道啊.....上图里的value是我用下面的方法获取的 MySqlConnection con = new MySqlConnecti ...

  7. 转【翻译】怎样在Ubuntu 12.04上配置Apache SSL证书

    关于SSL证书 SSL证书是加密网站信息和创建一个更安全的连接的一种方式.另外,证书能够向网站訪问者展示VPS的身份信息. 证书颁发机构颁发SSL证书.用来验证server的具体信息,而一个自签名的证 ...

  8. protobuf语法

    是什么? 目前市面上的unity手游开发主流数据通讯协议的解决方案.protobuf是google提供的一个开源序列化框架,类似于XML,JSON这样的数据表示语言,其最大的特点是基于二进制,因此比传 ...

  9. CString TCHAR互相转换

    CString->TCHAR*的转化可以用函数GetBuffer() // 原型:LPTSTR GetBuffer( int nMinBufLength ); CString str(_T(&q ...

  10. HTML&CSS精选笔记_CSS高级技巧

    CSS高级技巧 CSS精灵技术 需求分析 CSS精灵是一种处理网页背景图像的方式.它将一个页面涉及到的所有零星背景图像都集中到一张大图中去,然后将大图应用于网页,这样,当用户访问该页面时,只需向服务发 ...