在我的android-app中,我正在尝试创建一个包含两行文本的按钮。一个将显示单击(短)时按钮正在执行的操作,另一个应显示长按时的效果,并且可以长按该按钮。就像在android系统的标准键盘上一样(见picture)。
我尝试了什么:
Bitmap canvasBitmap = Bitmap.createBitmap(64, 24, Bitmap.Config.ARGB_8888);
Canvas imageCanvas = new Canvas(canvasBitmap);
Paint imagePaint = new Paint();
imagePaint.setTextAlign(Align.CENTER);
imagePaint.setTextSize(14f);
imageCanvas.drawText(String, pxwidth/2, pxheight, imagePaint);
image = new BitmapDrawable(canvasBitmap);
((TextView) findViewById(R.id.button)).setCompoundDrawables(null,image,null,null);
但我的当前API版本(API 9 / GINGERBREAD / Android 2.3)没有支持 你能帮帮我吗?
答案 0 :(得分:1)
如果您需要定位9,那么您无法使用该功能,并且您需要使用其他技术。我能想到的一种方法是使用ImageButton代替按钮,预渲染整个按钮的图像,然后将其应用到xml中的新ImageButton。
答案 1 :(得分:0)
我用这个Custom View
作为解决方案(感谢Gabe Sechan的想法):
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.widget.Button;
class LongClickButton extends Button {
private String mLongClickString;
private String mOnLongClick;
private Paint mTextPaint;
public LongClickButton(Context context, AttributeSet attrs){
super(context, attrs);
mTextPaint = new Paint();
mTextPaint.setTextSize(this.getTextSize()/2);
mTextPaint.setColor(Color.DKGRAY);
mTextPaint.setShadowLayer(0, 1, 1, Color.GRAY);
mTextPaint.setTextAlign(Align.RIGHT);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LongClickButton, 0, 0);
try{
mLongClickString = a.getString(R.styleable.LongClickButton_longClickText);
mOnLongClick = a.getString(R.styleable.LongClickButton_onLongClick);
} finally {
a.recycle();
}
}
public String getLongClickString(){
return mLongClickString;
}
public String getOnLongClick(){
return mOnLongClick;
}
public void setLongClickString(String LongClickString){
mLongClickString = LongClickString;
invalidate();
requestLayout();
}
public void setOnLongClick(String OnLongClick){
mOnLongClick = OnLongClick;
invalidate();
requestLayout();
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawText(mLongClickString, this.getWidth()-(this.getPaddingRight()/4*3), (this.getPaddingTop()/2)+mTextPaint.getTextSize(), mTextPaint);
}
}