如何在TextView中为文本创建边框?

时间:2016-07-15 13:23:14

标签: android textview

我想在TextView中为我的文字制作边框。我尝试为我找到解决方案,但我发现只有阴影 - 它看起来像:

enter image description here

但我想这样:

enter image description here

如何为文字制作边框?

1 个答案:

答案 0 :(得分:12)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CoustomTextView coustomTextView = (CoustomTextView) findViewById(R.id.pager_title);
    }
}

<强>用法:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@mipmap/background">


    <pk.sohail.gallerytest.activity.CoustomTextView
        android:id="@+id/pager_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/txt_title_photo_gallery"
        android:textColor="@color/white"
        android:textSize="30dp"
        android:textStyle="bold"
        app:outerShadowRadius="10dp"
        app:strokeColor="@color/title_text_color"
        app:strokeJoinStyle="miter"
        app:strokeWidth="2dp" />

</RelativeLayout>

<强>布局:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="CoustomTextView">

        <attr name="outerShadowRadius" format="dimension" />
        <attr name="strokeWidth" format="dimension" />
        <attr name="strokeMiter" format="dimension" />
        <attr name="strokeColor" format="color" />
        <attr name="strokeJoinStyle">
            <enum name="miter" value="0" />
            <enum name="bevel" value="1" />
            <enum name="round" value="2" />
        </attr>
    </declare-styleable>

</resources>

<强> attars:

CoustomTextView mtxt_name = (CoustomTextView) findViewById(R.id.pager_title);

<强>编程:

setStroke();

在致电setText();

之前使用if (fuBlogImageFile.ContentType == "image/jpeg") { //its a jpeg } 方法