在Android中,我有一个TextView,它的内容将是动态的。我想在每行文字后面显示一条水平线。我经常搜索并找到了EditText(How to use ruled/horizontal lines to align text in EditText in Android?)。我正在计划绘制动态TextView和它们下面的水平线。但我不知道怎样才能检测出行尾。任何帮助将受到高度赞赏。我希望与How to use ruled/horizontal lines to align text in EditText in Android?
的附加图像具有相同的效果答案 0 :(得分:5)
我正在使用 EditText 中每行文字之间绘制线条的技巧,然后通过设置 setKeyListener(我将使 EditText 不可编辑) null)到自定义EditText对象,以便EditText就像 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 并使其不可编辑。
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),那么它只是没有听到键但是 用户可以在EditText上看到一个光标。如果您不想要此光标,只需通过添加以下行禁用EditText:
et.setEnabled(false);
答案 1 :(得分:1)
您可以使用视图绘制线:对于水平线:
<View
android:layout_height="1dp"
android:layout_width="fill_parent"
android:background="#ffffff" />
答案 2 :(得分:0)
<TextView
android:layout_height="1dp"
android:layout_width="fill_parent"/>