【问题标题】:Android - Way to appear bordered text on the TextView?Android - 在 TextView 上显示带边框的文本的方式?
【发布时间】:2010-01-08 10:24:10
【问题描述】:

有没有办法在 TextView 上显示带边框的文本?

【问题讨论】:

  • 你能澄清一下“有边框的文字”是什么意思吗?
  • 感谢回复。我的意思是每个文本字母都会有边框。怎么做?对不起我的英语不好。

标签: android user-interface custom-controls border


【解决方案1】:

我建议扩展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>

【讨论】:

    【解决方案2】:

    最简单的方法是使用 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" />
    

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 2012-09-03
      • 2014-10-03
      • 2014-05-15
      • 1970-01-01
      • 2010-12-15
      相关资源
      最近更新 更多