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


当前回答

您可以为文本视图创建自定义背景。步骤

转到您的项目。转到资源,右键单击可绘制。单击新建->可绘制资源文件为您的文件命名在文件中粘贴以下代码

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="1dp"
        android:color="@color/colorBlack" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
    <corners android:radius="6dp" />
    <solid android:color="#ffffffff" />
</shape>

对于要将其用作背景的文本视图,android:background=“@drawbable/your_fileName”

其他回答

检查下面的链接以制作圆角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>

我有一个非常简单的方法,我想分享一下。

当我想调整文本视图时,我只需将它们放在LinearLayout中。我设置了LinearLayout的背景色,并为TextView添加了边距。结果就像是将TextView平方一样。

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

使用可绘制的形状

将以下内容另存为可绘制文件夹中的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的水平线?

可以通过两种方法设置边框。一种是可绘制的,另一种是编程的。

使用Drawable

<shape>
    <solid android:color="@color/txt_white"/>
    <stroke android:width="1dip" android:color="@color/border_gray"/>
    <corners android:bottomLeftRadius="10dp"
             android:bottomRightRadius="0dp"
             android:topLeftRadius="10dp"
             android:topRightRadius="0dp"/>
    <padding android:bottom="0dip"
             android:left="0dip"
             android:right="0dip"
             android:top="0dip"/>
</shape>

程序化的


public static GradientDrawable backgroundWithoutBorder(int color) {

    GradientDrawable gdDefault = new GradientDrawable();
    gdDefault.setColor(color);
    gdDefault.setCornerRadii(new float[] { radius, radius, 0, 0, 0, 0,
                                           radius, radius });
    return gdDefault;
}

试试看:

<shape>
    <solid android:color="@color/txt_white"/>
    <stroke android:width="1dip" android:color="@color/border_black"/>
</shape>