Android实现自定义带文字和图片Button的方法
作者:海 子 发布时间:2021-06-20 17:13:50
标签:Android,自定义,Button
本文实例讲述了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();
}
});
三种不同方法最后的运行效果:
完整实例代码点击此处本站下载。
希望本文所述对大家的Android程序设计有所帮助。


猜你喜欢
- 题目:使用栈计算类似表达式:5+2*3-2 的计算结果 提示:简易计算器操作符号限于+,-,*,/的计算分析思路:1、
- 在实际应用中,很可能我们希望自己的app在按下返回键的时候并不退出,而是像按home键一样仅仅返回桌面,而程序仍然在后台运行着。要怎么实现这
- 1.概述在之前的博文中简单介绍过如何实现fragment之间的信息交互:《Android中Fragment与Activity之间的交互(两种
- android 点击EditText始终不弹出软件键盘场景描述:正常情况下,当点击EditText时,软键盘会弹出来。现在的要求
- 一、前言在日常工作中,如果涉及到与第三方进行接口对接,有的会使用WebService的方式,这篇文章主要讲解在.NET Framework中
- 本文实例为大家分享了C#实现温度转换功能的具体代码,供大家参考,具体内容如下界面图代码using System;using System.C
- 本文介绍的库中的侧滑效果借鉴自SwipeMenu,并对SipwMenu的源码做了修改与Bug修复,然后才开发出的SwipeRecyclerV
- Gstreamer到底是个啥?GStreamer 是一个 基于pipeline的多媒体框架,基于GObject,以C语言写成。应用GStre
- 本文实例为大家分享了java后台批量下载文件并压缩成zip下载的具体代码,供大家参考,具体内容如下因项目需要,将服务器上的图片文件压缩打包z
- 本文实例讲述了使用SAX来解析XML。通常来说在Android里面可以使用SAX和DOM,DOM需要把整个XML文件读入内存再解析,比较消耗
- Android 应用签名的两种方法一、使用pem签名 (一) apk签名命令java –jar sign
- 1.URI与URLURI(Uniform Resource Identifier,统一资源标志符),表示web上的每一种可用资源,具体的东西
- 目录Mybatis简介Mybatis开发步骤:Mybatis的映射文件概述Mybatis的增删改查操作MyBatis的核心配置文件概述MyB
- SpringBoot找不到javax.servlet.Filter的问题新创建一个SpringBoot项目,编译时出现了找不到javax.s
- 1.获取签名与模板进入阿里云平台,进入短信服务模块,在以下位置添加签名和模板(格式一定按照要求填写 审批的比较严格)2.编写模板与签名的枚举
- Data Annotations是在Asp.Net中用于表单验证的,它通过Attribute直接标记字段的有效性,简单且直观。在非Asp.N
- java简易小游戏制作游戏思路:设置人物移动,游戏规则,积分系统,随机移动的怪物,游戏胜负判定,定时器。游戏内容部分package 代码部分
- 字符串操作是C#中最基本的、最常见的、也是用的最多的,以下我总结了几种常见的方法:1.把字符串按照分隔符转换成 List ///
- 最近的一个Android需要用到扫码功能,用的是Zxing开源库。Zxing的集成就不说了,但是Zxing默认的是横屏扫码,在实际生产中并不
- 目录开启定时任务注解@EnableScheduling@Scheduled添加定时任务Cron表达式在线cron工具适应场景springBo