Android的:在一个TextView绘制线Android、TextView

2023-09-13 23:46:31 作者:心岛未晴

我也问一个问题,previously标题:在Android:直纹/卧式线的TextView 。但我没有必要回答。所以我刨由java.Is有什么方法画线内的TextView TextView的上画线做这个?或者,如果我得到的java code线的末尾,然后我就可以添加TextView中的每一行。任何帮助将是非常美联社preciated。

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(空)可自定义的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);
    }
} 

现在使用的对象的 LinedEditText 类,你需要您的的TextView 并使其不可编辑的。

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(空)使EditText上不可编辑的话,它就像一个TextView。

android制作一个可以旋转textview肿么实现

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

如果您使用 et.setKeyListener(空)才把它只是不听键,但 用户可以看到光标在的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);