是否可以在Android TextView周围绘制边框?


当前回答

创建一个边框视图,背景颜色作为边框的颜色和文本视图的大小。将边框视图填充设置为边框的宽度。将文本视图背景色设置为文本视图所需的颜色。现在在边框视图中添加文本视图。

其他回答

我找到了一种更好的方法来在TextView周围添加边框。

使用九块图像作为背景。它非常简单,SDK附带了一个制作9补丁图像的工具,而且它绝对不需要编码。

链接是http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-补丁。

事实上,这很简单。如果您想要在Textview后面有一个简单的黑色矩形,只需在Textview标记中添加android:background=“@android:color/black”即可。这样地:

<TextView
    android:textSize="15pt" android:textColor="#ffa7ff04"
    android:layout_alignBottom="@+id/webView1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@android:color/black"/>

检查下面的链接以制作圆角http://androidcookbook.com/Recipe.seam?recipeId=2318

Android项目中res下的可绘制文件夹不限于位图(PNG或JPG文件),但它也可以保存XML文件中定义的形状。

然后可以在项目中重用这些形状。形状可用于在布局周围放置边框。此示例显示了带有弯曲角的矩形边框。在可绘制文件夹中创建一个名为customerborder.xml的新文件(在Eclipse中,使用“文件”菜单并选择“新建”,然后选择“文件”,在文件名中键入可绘制文件夹并单击“完成”)。

输入定义边框形状的XML:

<?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="20dp"/>
    <padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
    <solid android:color="#CCCCCC"/>
</shape>

属性android:shape设置为矩形(形状文件也支持椭圆、直线和环形)。矩形是默认值,因此如果它是正在定义的矩形,则可以忽略此属性。有关形状的Android文档,请参见http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape有关形状文件的详细信息。

元素角点将矩形角点设置为圆角。可以在每个角落设置不同的半径(参见Android参考)。

填充属性用于移动应用形状的视图的内容,以防止内容与边境

此处的边框颜色设置为浅灰色(CCCCCC十六进制RGB值)。

形状也支持渐变,但这里没有使用渐变。再次,请参阅Android资源,了解如何定义渐变。使用android:background=“@drawbable/customborder”将形状应用于laypout。

在布局中,可以正常添加其他视图。在本例中,添加了一个TextView,文本为白色(FFFFFF十六进制RGB)。背景设置为蓝色,加上一些透明度以降低亮度(A00000FF十六进制alpha RGB值)。最后,通过将布局放置到另一个带有少量填充的布局中,将布局从屏幕边缘偏移。因此,完整布局文件为:

<?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"
              android:padding="5dp">
    <LinearLayout android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:background="@drawable/customborder">
        <TextView android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Text View"
                android:textSize="20dp"
                android:textColor="#FFFFFF"
                android:gravity="center_horizontal"
                android:background="#A00000FF" />
    </LinearLayout>
</LinearLayout>

简单的方法是为TextView添加视图。底部边框线示例:

<LinearLayout android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:text="@string/title"
        android:id="@+id/title_label"
        android:gravity="center_vertical"/>
    <View
        android:layout_width="fill_parent"
        android:layout_height="0.2dp"
        android:id="@+id/separator"
        android:visibility="visible"
        android:background="@android:color/darker_gray"/>

</LinearLayout>

对于其他方向的边框,请调整分隔符视图的位置。

让我总结一下几种不同的(非编程的)方法。

使用可绘制的形状

将以下内容另存为可绘制文件夹中的XML文件(例如my_border.XML):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- View background color -->
    <solid
        android:color="@color/background_color" >
    </solid>

    <!-- View border color and width -->
    <stroke
        android:width="1dp"
        android:color="@color/border_color" >
    </stroke>

    <!-- The radius makes the corners rounded -->
    <corners
        android:radius="2dp"   >
    </corners>

</shape>

然后将其设置为TextView的背景:

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_border" />

更多帮助:

形状可绘制(Android文档)Android开发者提示和技巧:XML绘图(第一部分)

使用9补丁

9补丁是可拉伸的背景图像。如果您制作带有边框的图像,那么它将为TextView提供边框。您需要做的就是制作图像,然后将其设置为TextView中的背景。

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_ninepatch_image" />

以下是一些链接,将展示如何制作9补丁图像:

绘制9个补丁简单九补丁生成器Android UI 9补丁的简单指南在Android中创建和使用9补丁图像

如果我只想要顶部边框呢?

使用图层列表

可以使用层列表将两个矩形堆叠在一起。通过使第二个矩形略小于第一个矩形,可以产生边框效果。第一个(下部)矩形是边框颜色,第二个矩形是背景颜色。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Lower rectangle (border color) -->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/border_color" />
        </shape>
    </item>

    <!-- Upper rectangle (background color) -->
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/background_color" />
        </shape>
    </item>
</layer-list>

设置android:top=“2dp”将顶部偏移2dp(使其变小)。这允许第一个(下部)矩形显示出来,从而产生边框效果。您可以将此应用于TextView背景,方法与上面绘制的形状相同。

以下是关于图层列表的更多链接:

了解Android的<层列表>如何在可绘制形状XML选择器中创建底部边框?在可绘制的xml中的android视图上创建三面边框?

使用9补丁

您只需制作一个带有单个边框的9补丁图像即可。其他一切都与上面讨论的相同。

使用视图

这是一种技巧,但如果需要在两个视图之间添加分隔符或在单个TextView中添加边框,则效果很好。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- This adds a border between the TextViews -->
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@android:color/black" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

以下是更多链接:

如何在Android中画线如何在活动中的编辑文本之间放置水平除数线如何在相对布局中的图像视图上方添加一条1px的水平线?