Android:Draw line on a textview

后端 未结 2 1956
粉色の甜心
粉色の甜心 2020-12-05 08:46

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.

相关标签:
2条回答
  • 2020-12-05 09:22

    I don't know why do you want a line inside a textview. If you want to draw a horizontal single line just put this code between the textviews that you are reading from the db, obviously in your layout xml file:

    <View android:background="#colorYouWant" android:layout_width = "fill_parent" android:layout_height="1dip" android:id="+@id/horizontalLine"/>

    Adjust the separation by java coding doing this in the related java code:

    View horizontalLine = (View)findViewById(R.id.horizontalLine);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(left, top, right, bottom);
    horizontalLine.setLayoutParams(lp);
    

    The left, top, right, bottom parameters are the ones that you have to change.

    0 讨论(0)
  • 2020-12-05 09:23

    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 :)


    A custom EditText that draws lines between each line of text that is displayed:

    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);
        }
    } 
    

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

    An Example:

    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) makes the EditText non-editable so, it acts like a TextView.


    The Output:

    enter image description here

    Issue with cursor:

    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);
    
    0 讨论(0)
提交回复
热议问题