android – 如何将文本添加到浮动 *** 作按钮?

android – 如何将文本添加到浮动 *** 作按钮?,第1张

概述我想在浮动 *** 作按钮而不是图像上显示文本.我尝试使用 android:contentDescription =“string”但 *** 作按钮似乎是空的我需要设置一些颜色或做任何其他事情.请让我知道. 在我们的项目中,我们使用名为TextDrawable的自定义类.这个类扩展了Drawable,在它的方法draw(Canvas)中,它简单地在画布上绘制文本.这个类适合我们的特定需求,但我认为,这个想法(主 我想在浮动 *** 作按钮而不是图像上显示文本.我尝试使用 android:contentDescription =“string”但 *** 作按钮似乎是空的我需要设置一些颜色或做任何其他事情.请让我知道.解决方法 在我们的项目中,我们使用名为TextDrawable的自定义类.这个类扩展了Drawable,在它的方法draw(Canvas)中,它简单地在画布上绘制文本.这个类适合我们的特定需求,但我认为,这个想法(主要是在draw()方法中会帮助你:

public class TextDrawable extends Drawable {protected final Paint textPaint;protected colorStateList color;protected String text;protected int iHeight;protected int iWIDth;protected int measureDWIDth,measuredHeight;private float ascent;/** * A flag whether the drawable is stateful - whether to redraw if the state of vIEw has changed */protected boolean stateful;/** * Vertical alignment of text */private VerticalAlignment verticalAlignment;.... some constructors...public TextDrawable(Context ctx,String text,colorStateList color,float textSize,VerticalAlignment verticalAlignment) {    textPaint = new Paint();    this.text = text;    initPaint();    this.textPaint.setTextSize(textSize);    measureSize();    setBounds(0,iWIDth,iHeight);    this.color = color;    textPaint.setcolor(color.getDefaultcolor());    this.verticalAlignment = verticalAlignment;}/** * Set bounds of drawable to start on coordinate [0,0] and end on coordinate[measureDWIDth,* measuredHeight] */public final voID setBoundsByMeasuredSize() {    setBounds(0,measureDWIDth,measuredHeight);    invalIDateSelf();}@OverrIDepublic boolean isstateful() {    return stateful;}public voID setStateful(boolean stateful) {    this.stateful = stateful;}private voID initPaint() {    textPaint.setAntiAlias(true);    textPaint.setTextAlign(Paint.Align.CENTER);}/** * Vertical alignment of text within the drawable (Horizontally it is always aligned to center */public VerticalAlignment getVerticalAlignment() {    return verticalAlignment;}/** * Vertical alignment of text within the drawable (Horizontally it is always aligned to center */public voID setVerticalAlignment(VerticalAlignment verticalAlignment) {    if (this.verticalAlignment != verticalAlignment) {        this.verticalAlignment = verticalAlignment;        invalIDateSelf();    }}/** * displayed text */public String getText() {    return text;}/** * displayed text */public voID setText(String text) {    if (this.text == null || !this.text.equals(text)) {        this.text = text;        invalIDateSelf();    }}/** * The color of text */public colorStateList getcolor() {    return color;}/** * The color of text */public voID setcolor(colorStateList colorStateList) {    if (this.color == null || !this.color.equals(colorStateList)) {        this.color = colorStateList;        invalIDateSelf();    }}/** * The color of text */public voID setcolor(int color) {    setcolor(colorStateList.valueOf(color));}/** * Text size */public voID setTextSize(float size) {    if (this.textPaint.getTextSize() != size) {        this.textPaint.setTextSize(size);        measureSize();        invalIDateSelf();    }}/** * Text size */public voID setTextSize(int unit,float size,Context context) {    setTextSize(TypedValue.applyDimension(unit,size,context.getResources().getdisplayMetrics()));}/** * This method is called by default when any property that may have some influence on the size * of drawable This method should use measureDWIDth and measuredHeight propertIEs to store the * measured walues By default the measureDWIDth and measuredHeight are set to iWIDth and iHeight * (size of text) by this method. */protected voID measureSize() {    ascent = -textPaint.ascent();    iWIDth = (int) (0.5f + textPaint.measureText(text));    iHeight = (int) (0.5f + textPaint.descent() + ascent);    measureDWIDth = iWIDth;    measuredHeight = iHeight;}public float getTextSize() {    return textPaint.getTextSize();}@OverrIDeprotected boolean onStateChange(int[] state) {    int clr = color != null ? color.getcolorForState(state,0) : 0;    if (textPaint.getcolor() != clr) {        textPaint.setcolor(clr);        return true;    } else {        return false;    }}public Typeface getTypeface() {    return textPaint.getTypeface();}public voID setTypeface(Typeface typeface) {    if (!textPaint.getTypeface().equals(typeface)) {        textPaint.setTypeface(typeface);        invalIDateSelf();    }}/** * The method is called before the text is drawn. This method can be overrIDden to draw some background (by default this method does nothing). * @param canvas The canvas where to draw. * @param bounds The bounds of the drawable. */protected voID drawBefore(Canvas canvas,Rect bounds) {}/** * The method is called after the text is drawn. This method can be overrIDen to draw some more graphics over the text (by default this method does nothing). * @param canvas The canvas where to draw. * @param bounds The bound of the drawable. */protected voID drawAfter(Canvas canvas,Rect bounds) {}@OverrIDepublic voID draw(Canvas canvas) {    if (text == null || text.isEmpty()) {        return;    }    final Rect bounds = getBounds();    int stack = canvas.save();    canvas.translate(bounds.left,bounds.top);    drawBefore(canvas,bounds);    if (text != null && !text.isEmpty()) {        final float x = bounds.wIDth() >= iWIDth ? bounds.centerX() : iWIDth * 0.5f;        float y = 0;        switch (verticalAlignment) {            case BASEliNE:                y = (bounds.height() - iHeight) * 0.5f + ascent;                break;            case top:                y = bounds.height();                break;            case BottOM:                y = bounds.height();                break;        }        canvas.drawText(text,x,y,textPaint);    }    drawAfter(canvas,bounds);    canvas.restoretoCount(stack);}@OverrIDepublic voID setAlpha(int Alpha) {    if (textPaint.getAlpha() != Alpha) {        textPaint.setAlpha(Alpha);        invalIDateSelf();    }}@OverrIDepublic voID setcolorFilter(colorFilter cf) {    if (textPaint.getcolorFilter() == null || !textPaint.getcolorFilter().equals(cf)) {        textPaint.setcolorFilter(cf);        invalIDateSelf();    }}@OverrIDepublic int getopacity() {    return PixelFormat.TRANSLUCENT;}public enum VerticalAlignment {    top,BottOM,BASEliNE}

以及如何使用它:

fab.setimageDrawable(new TextDrawable(fab.getContext(),"FAB",colorStateList.valueOf(color.BLACK),32.f,VerticalAlignment.BASEliNE));

(fab是floatingActionbutton)

@H_419_30@ 总结

以上是内存溢出为你收集整理的android – 如何将文本添加到浮动 *** 作按钮?全部内容,希望文章能够帮你解决android – 如何将文本添加到浮动 *** 作按钮?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1130231.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存