Is there any way to put a line over text in Android? Since underlines and strikethroughs are possible (independent of font), it seems that overlines should be too.

I've tried using the combining overline symbol (pictured below), but it definitely looks funky. Is there a different way to achieve this?

Roman Numeral Converter

有帮助吗?

解决方案

I know this is not a perfect solution. but it will give some Idea...

public class OverLineTextView extends TextView {

    private Paint paint;

    public OverLineTextView(Context context) {
        super(context);
        init();
    }

    public OverLineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public OverLineTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
        paint.setStyle(Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float width = getPaint().measureText(getText().toString());
        canvas.drawLine(getTotalPaddingLeft(), getTotalPaddingTop() + 1,
                getTotalPaddingLeft() + width, getTotalPaddingTop() + 1, paint);
    }
}

其他提示

After doing enough R & D, the only way for doing that is to create a ImageView and set it dynamically to a line image and set its width with respect to the width of the textview by using getWidth() and position above the textview and align left with respect to the textview

If you want a strikethrough on your textview ,you can do using paint flags

TextView textview1=(TextView) findViewById(R.id.textView1);
textview1.setPaintFlags(textview1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top