Question: EditText control: the need for multiple lines, and each line has a dividing line isolated, how do? ?
Let us talk about my own practice:
1, with the base map method, but this does not solve a problem: When scrolling, the base map of the dividing line is not to follow the movement of
fonts2, rewrite control, underline mode, there are problems to be solved: the underlined text only exists when only the presence underscores, no text, just a line on a; Furthermore, underlined font so close now, and pretty
Find a solution, thank you
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Who help solve what ah, engage in a few days, did not solve it
Reply:
Rewrite onDraw method. EditText painting directly on the line, should be able to achieve
Reply:
Do not know ah, there's no point talking about carefully ah
Reply:
mark expressed concern
Reply:
Nobody paid attention, hey
Reply:
positive solution, you need to draw smoke inside the painting, to calculate the position of each line of text is located, and then underlined
Reply:
Reply:
Rewrite onDraw method. EditText painting directly on the line, should be able to achieve
Reply:
I'll give you a piece of code
public static 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; iint baseline = getLineBounds (i, r);
/ / Here is a horizontal line drawn
canvas.drawLine (r.left, baseline + 1, r.right, baseline + 1,
paint);
}
super.onDraw (canvas);
}
}
You go under careful study, what do not understand, you can ask me
Reply:
Complemented upstairs, int count = getLineCount ();
This is to get the number of rows
For example, you use this custom control setText (); android will display a few lines, so you can get into a few lines to be displayed
Reply:
I am currently doing, but there are problems: the first time you show only one line, the second line needs to be able to demonstrate underlined
Reply:
I am currently doing, but there are problems: the first time you show only one line, the second line needs to be able to demonstrate underlined
Reply:
Positive solution!
Reply:
I do not think that when the text is displayed on the entire screen line by line, how to count the number of rows ah?
Reply:
Four months, you can still concerned about what
Reply:
My idea is that you get hold of the background image of the first piece of writing covered, and then as written in accordance with the 9th floor, so when there is shading your initial write more when there are not on the shading. . Although trouble spots. . But at least completion. .
Reply:
Not the background, is a base map. . . . Said background looks very professional. Oh

Reply:
Gets the total height of the control, and then Xu height of each row, you can come to draw the number of rows. Here is the reference code:
public class WriteText extends EditText {
private static final int MARGIN = 10;
private Rect mRect;
private Paint mPaint;
public WriteText (Context context, AttributeSet attrs) {
super (context, attrs);
mRect = new Rect ();
mPaint = new Paint ();
mPaint.setStyle (Paint.Style.STROKE);
mPaint.setColor (0xFF80ADB7);
}
@ Override
protected void onDraw (Canvas canvas) {
super.onDraw (canvas);
Rect r = mRect;
Paint paint = mPaint;
Layout layout = getLayout ();
if (! canvas.getClipBounds (r)) {
return;
}
float startX = r.left + MARGIN, stopX = r.right - MARGIN;
int count = layout.getLineCount ();
int lineHeight = getLineHeight ();
int height = getHeight () - getPaddingBottom () - getPaddingTop ();
int n = height% lineHeight == 0 height / lineHeight: height / lineHeight + 1;
?if (countcount = n;
}
for (int i = 1; i <= count; i + +) {
/ * Int baseline = layout.getLineBaseline (i);
int bottom = layout.getLineBottom (i);
startY = stopY = (baseline + bottom + 10) / 2;
canvas.drawLine (startX, startY, stopX, stopY, paint); * /
int y = lineHeight * i;
canvas.drawLine (startX, y, stopX, y, paint);
}
}
}
Reply:
# 18 is to force. . . A considerable force
No comments:
Post a Comment