今天在整理资料的时候发现有个问题,原来做的些软件中用到的按钮,要么是纯文字的,要么是纯图片的
突然看有款软件上的按钮文字可以同时显示,
<!--StartFragment -->
而且我用hierarchyviewer查看了下
他确确实实是图片加文字的,所以问了下群友并且思考了下,总有以下解决方法
1.直接用图片和文字的截图放在Imagebutton上显示,这种事最简单但是也是最占资源,以后修改和国家化都是最不方便的方法
2.用button上设置图片的方法可以,这种简单,并且以后很容易修改<!--StartFragment -->
<Buttonandroid:drawableTop="@drawable/****">
3.还有个方法也可以实现相同的效果
textview.setCompoundDrawables(null, icon, null, null);
4.还有一种比较基数的方法啊,就是自定一个类似button的空间实现图片和文字的共存
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.Rect;
importandroid.view.MotionEvent;
importandroid.view.View;
publicclassImageTextButtonextendsView{
privatefinalstaticintWIDTH_PADDING=8;
privatefinalstaticintHEIGHT_PADDING=10;
privatefinalstaticintSPACE=10;
privatefinalStringlabel;
privatefinalintimageResId;
privatefinalBitmapimage;
privateintfontWidth;
privateintfontHeight;
publicImageTextButton(finalContextc,intrid,Stringtext){
super(c);
this.label=text;
this.imageResId=rid;
this.image=BitmapFactory.decodeResource(c.getResources(),imageResId);
setFocusable(true);
setClickable(true);
getFontWidthAndHeight();
}
privatevoidgetFontWidthAndHeight(){
PaintpFont=newPaint();
Rectrect=newRect();
pFont.getTextBounds("信",0,1,rect);
this.fontHeight=rect.height();
this.fontWidth=rect.width();
}
privateintgetTextWidth(Stringtext){
returntext.length()*this.fontWidth;
}
@Override
protectedvoidonFocusChanged(booleangainFocus,intdirection,
RectpreviouslyFocusedRect){
if(gainFocus==true){
this.setBackgroundColor(Color.rgb(255,165,0));
}
else{
this.setBackgroundColor(Color.alpha(0));
}
}
@Override
protectedvoidonDraw(Canvascanvas){
PainttextPaint=newPaint();
textPaint.setColor(Color.WHITE);
canvas.drawBitmap(image,WIDTH_PADDING/2,HEIGHT_PADDING/2,null);
canvas.drawText(label,(image.getWidth()-getTextWidth(label)/2)/2,(HEIGHT_PADDING/2)+
image.getHeight()+8+SPACE,textPaint);
}
@Override
publicbooleanonTouchEvent(MotionEventevent){
intaction=event.getAction();
switch(action){
caseMotionEvent.ACTION_DOWN:
caseMotionEvent.ACTION_MOVE:
this.setBackgroundColor(Color.rgb(255,165,0));
break;
caseMotionEvent.ACTION_UP:
caseMotionEvent.ACTION_CANCEL:
this.setBackgroundColor(Color.alpha(0));
break;
}
returnsuper.onTouchEvent(event);
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
}
privateintmeasureWidth(intmeasureSpec){
intpreferred=image.getWidth()*2;
returngetMeasurement(measureSpec,preferred);
}
privateintmeasureHeight(intmeasureSpec){
intpreferred=image.getHeight()+this.fontHeight+SPACE*2;
returngetMeasurement(measureSpec,preferred);
}
privateintgetMeasurement(intmeasureSpec,intpreferred){
intspecSize=MeasureSpec.getSize(measureSpec);
intmeasurement=0;
switch(MeasureSpec.getMode(measureSpec)){
caseMeasureSpec.EXACTLY:
measurement=specSize;
break;
caseMeasureSpec.AT_MOST:
measurement=Math.min(preferred,specSize);
break;
default:
measurement=preferred;
break;
}
returnmeasurement;
}
publicStringgetLabel(){
returnlabel;
}
publicintgetImageResId(){
returnimageResId;
}
}
只是简单的总结,不是很详细,希望大家看完后多多补充