我从很多天开始尝试,但没有得到任何解决方案。我已经参考了这个 Android:在文本视图上绘制线 我试图在文本视图中创建虚线,完成它,工作正常,但我想添加虚线直到文本视图包含的文本长度,如果文本视图有一行文本然后它工作正常但是如果文本视图有多行文本,那么对于文本一半或小于屏幕宽度的行,那么虚线也会出现我不想要...。我使用画图绘制虚线...请帮助我....在此先感谢
问问题
2580 次
1 回答
3
靠着题目,你在里面做的,试着写在类的构造函数里LinedEditText
mPaint = new Paint();
mPaint.setARGB(255, 0, 0, 0);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float[] {10,10}, 0));
代替
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x800000FF);
编辑:
请再告诉我一次,你想要这样吗:
或这个:
编辑:
主要活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
String text = "Sample Sample Sample Sample Sample Sample Sample Sample Sample Sample";
LinedEditText et = new LinedEditText(this, null, text);
et.setText(text);
et.setLayoutParams(textViewLayoutParams);
et.setKeyListener(null);
ll.addView(et);
this.setContentView(ll);
}
内衬编辑文本:
public class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;
String text;
public LinedEditText(Context context, AttributeSet attrs, String text) {
super(context, attrs);
this.text = text;
mRect = new Rect();
mPaint = new Paint();
mPaint.setARGB(255, 0, 0, 0);
mPaint.setStyle(Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float[] { 10, 10 }, 0));
}
@Override
protected void onDraw(Canvas canvas) {
Rect r = mRect;
Paint paint = mPaint;
int lineCount = getLineCount();
int size = getLayout().getLineStart(lineCount-1);
String str = getText().toString().substring(size);
float densityMultiplier = getContext().getResources().getDisplayMetrics().density;
float scaledPx = 20 * densityMultiplier;
paint.setTextSize(scaledPx);
float i = paint.measureText(str);
for (int k = 0; k < lineCount-1; k++) {
int baseline = getLineBounds(k, r);
canvas.drawLine(r.left, baseline + 2, r.right, baseline + 2, paint);
}
int baseline = getLineBounds(lineCount-1, r);
canvas.drawLine(r.left, baseline + 2, i, baseline + 2, paint);
super.onDraw(canvas);
}
}
于 2013-08-30T07:35:01.073 回答