【Android】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】Android实现自定义带文字和图片的Button,布布扣,bubuko.com

时间: 2024-08-05 15:24:57

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

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

在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最小.在Button的属性中有一个是drawableLeft,这个 属性可以把图片设置在文字的左边,但是这种方式必须让icon的背景色是透明的,如果icon的背景色不是透明的话,会导致点击按钮时icon部分的背景 色不会发生变化. 主要代码: <Button android:id="@+id/b

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

由于SDK提供的ImageButton只能添加图片,不能添加文字:而Button控件添加的文字只能显示在图片内部:当我们需要添加文字在图片外部时就不能满足我们的需求了,顾只能自己写个自定义ImageButton.说是ImageButton,其实并不是继承于ImageButton,而是从LinearLayout继承,由于LinearLayout是线性排列,通过setOrientation(LinearLayout.VERTICAL)的方式达到View垂直排列的目的,所以很简单,只需要添加两个Vie

Android开发之自定义TabHost文字及背景(源代码分享)

使用TabHost 可以在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该如何进行自定义修改优化呢 MainActivity的源代码 package com.dream.ledong; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Gr

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

自定义标题栏在很多的android app中很常见,可以说是一种很有用的UI设计方法.自 己也本着学习的态度,经过一番各种坑,终于实现了,现总结如下: 一:大致流程 1.      对指定的android activity设置自定义主题风格,其中自定义主题风格是关键 在android 4.0以上版本中如果使用Theme.Holo或者Theme.Light等,程序会 一直报错误-you cannot combine custom title with other feature titles 2.

Android自定义组合控件--图片加文字,类似视频播放软件的列表

分四步来写: 1,组合控件的xml; 2,自定义组合控件的属性; 3,自定义继承组合布局的class类,实现带两参数的构造器; 4,在xml中展示组合控件. 具体实现过程: 一.组合控件的xml 我接触的有两种方式,一种是普通的Activity的xml:一种是父节点为merge的xml.我项目中用的是第一种,但个人感觉第二种好,因为第一种多了相对或者绝对布局层. 我写的 custom_pictext.xml <?xml version="1.0" encoding="u

Android系统自带分享功能的实现(可同时分享文字和图片)

 /** * 分享功能 * * @param context * 上下文 * @param activityTitle * Activity的名字 * @param msgTitle * 消息标题 * @param msgText * 消息内容 * @param imgPath * 图片路径,不分享图片则传null */ public void shareMsg(String activityTitle, String msgTitle, String msgText, String img

Android 自定义带刻度的seekbar

自定义带刻度的seekbar 1.布局 <span style="font-family:SimHei;font-size:18px;"><com.imibaby.client.views.CustomSeekbar android:id="@+id/myCustomSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_cont

自定义带图片和文字的ImageTextButton

今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学习.下一次或者过几天我会从自定义属性,在布局文件中使用属性的方式再讲一篇关于自定义控件的文章,希望对大家能够有所帮助. 现在开始讲自定义带图片和文字的ImageTextButton的实现方法. 效果图如下: 第一步:新建一个image_text_buttton.xml的布局文件,供自定义的控件使用

文字转图片APP,Android版本(长微博手机版)超越长微博

文字转图片,目前支持自定义生成图片尺寸(默认为手机宽度*自适应高度),支持字体大小,颜色,背景色,背景图,字体,行间距设置,以及自定义水印.并且增加了二维码功能和文件导入生成功能,其中二维码功能用于文字生成二维码后,使用文字转图片二维码扫描后,会立即生成对应的文字图片,支持加密!自定义密钥等功能,后期的创新功能陆续开发中! 开发者:谭东. 顶尖文字转图片技术!更多功能开发中 下载地址:http://pan.baidu.com/s/1qWHIPru 联系QQ :85204173 下载地址:http