Android:在文本视图上画线 [英] Android:Draw line on a textview

查看:15
本文介绍了Android:在文本视图上画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾在 Android:Textview 中的规则/水平线.但我没有得到要求的答案.所以我打算通过java在TextView上画线来做到这一点.有没有办法在TextView中画线?或者如果我从 java 代码中获得行尾,那么我将能够为每一行添加 textview.任何帮助将不胜感激.

I have asked a question previously titled "Android: Ruled/horizonal lines in TextView" at Android: Ruled/horizonal lines in Textview . But I haven't got required answer. So I'm planing to do this by drawing line on TextView by java.Is there any way to draw Line inside TextView? or if I get end of line from java code then I'll be able to add textview for each line. Any help will be highly appreciated.

推荐答案

我在 EditText 中使用了在每一行文本之间画线的技术,然后我将制作 EditText 通过将 setKeyListener(null) 设置为自定义 EditText 对象,使 EditText 的行为类似于 TextView :)

I'm using the technique of drawing lines between each line of text in EditText and then I will make the EditText non-editable by setting setKeyListener(null) to the custom EditText object so that, the EditText acts like a TextView :)

public class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x800000FF);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int count = getLineCount();
        Rect r = mRect;
        Paint paint = mPaint;

        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, r);

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        }

        super.onDraw(canvas);
    }
} 

<小时>

现在在您需要 TextView 的地方使用 LinedEditText 类的对象并使其不可编辑.


Now use object of LinedEditText class where you need your TextView and make it non-editable.

public class HorizontalLine extends Activity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);
        setTitle("Android: Ruled/horizonal lines in Textview");

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        LinedEditText et = new LinedEditText(this, null);
        et.setText("The name of our country is Bangladesh. I am proud of my country :)");
        et.setLayoutParams(textViewLayoutParams);
        et.setKeyListener(null);

        ll.addView(et);
        this.setContentView(ll);

    }

}

<小时>

et.setKeyListener(null) 使 EditText 不可编辑,因此它的作用类似于 TextView.


et.setKeyListener(null) makes the EditText non-editable so, it acts like a TextView.

如果你只使用 et.setKeyListener(null) 那么它只是不听键,而是用户可以在 EditText 上看到一个光标.如果您不想要此光标,只需添加以下行即可禁用 EditText:

If you use et.setKeyListener(null) only then it is just not listening to keys but user can see a cursor on the EditText. If you don't want this cursor just disable the EditText by adding this line:

 et.setEnabled(false);

这篇关于Android:在文本视图上画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆