【发布时间】:2010-01-08 10:24:10
【问题描述】:
有没有办法在 TextView 上显示带边框的文本?
【问题讨论】:
-
你能澄清一下“有边框的文字”是什么意思吗?
-
感谢回复。我的意思是每个文本字母都会有边框。怎么做?对不起我的英语不好。
标签: android user-interface custom-controls border
有没有办法在 TextView 上显示带边框的文本?
【问题讨论】:
标签: android user-interface custom-controls border
我建议扩展TextView
见Android Custom Component Guide
package samples.test;
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect = new Rect();
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3);
getLocalVisibleRect(rect);
canvas.drawRect(rect, paint);
}
}
比在布局xml中使用它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<samples.test.MyTextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>
【讨论】:
最简单的方法是使用 9 补丁背景。
<TextView android:id="@+id/txt_target"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="000000000"
android:gravity="center_vertical"
android:background="@drawable/textfield_default"
android:layout_marginRight="5dip" />
【讨论】: