如何在 Android 文本视图周围放置边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3496269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I put a border around an Android textview?
提问by yamspog
Is it possible to draw a border around a textview?
是否可以在 textview 周围绘制边框?
回答by Konstantin Burov
You can set a shape drawable (a rectangle) as background for the view.
您可以设置一个可绘制的形状(一个矩形)作为视图的背景。
<TextView android:text="Some text" android:background="@drawable/back"/>
And rectangle drawable back.xml (put into res/drawable folder):
和矩形 drawable back.xml(放入 res/drawable 文件夹):
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/white" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
You can use @android:color/transparent
for the solid color to have a transparent background.
You can also use padding to separate the text from the border.
for more information see: http://developer.android.com/guide/topics/resources/drawable-resource.html
您可以使用@android:color/transparent
纯色来获得透明背景。您还可以使用填充将文本与边框分开。有关更多信息,请参阅:http: //developer.android.com/guide/topics/resources/drawable-resource.html
回答by Suragch
Let me summarize a few different (non-programmatic) methods.
让我总结一些不同的(非程序化的)方法。
Using a shape drawable
使用形状可绘制
Save the following as an XML file in your drawable folder (for example, my_border.xml):
将以下内容另存为可绘制文件夹中的 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>
Then just set it as the background to your TextView:
然后将其设置为 TextView 的背景:
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_border" />
More help:
更多帮助:
Using a 9-patch
使用 9-patch
A 9-patch is a stretchable background image. If you make an image with a border then it will give your TextView a border. All you need to do is make the image and then set it to the background in your TextView.
9-patch 是可拉伸的背景图像。如果您制作带有边框的图像,那么它会给您的 TextView 一个边框。您需要做的就是制作图像,然后在 TextView 中将其设置为背景。
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_ninepatch_image" />
Here are some links that will show how to make a 9-patch image:
以下是一些链接,将展示如何制作 9 块图像:
- Draw 9-patch
- Simple Nine-patch Generator
- A simple guide to 9-patch for Android UI
- Creating & Using 9-patch images in Android
What if I just want the top border?
如果我只想要顶部边框怎么办?
Using a layer-list
使用图层列表
You can use a layer list to stack two rectangles on top of each other. By making the second rectangle just a little smaller than the first rectangle, you can make a border effect. The first (lower) rectangle is the border color and the second rectangle is the background color.
您可以使用图层列表将两个矩形堆叠在一起。通过使第二个矩形比第一个矩形小一点,您可以制作边框效果。第一个(下方)矩形是边框颜色,第二个矩形是背景色。
<?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>
Setting android:top="2dp"
offsets the top (makes it smaller) by 2dp. This allows the first (lower) rectangle to show through, giving a border effect. You can apply this to the TextView background the same way that the shape
drawable was done above.
设置android:top="2dp"
将顶部偏移(使其更小)2dp。这允许第一个(下方)矩形显示出来,从而产生边框效果。您可以将它应用到 TextView 背景,就像shape
上面完成 drawable一样。
Here are some more links about layer lists:
以下是有关图层列表的更多链接:
- Understanding Android's <layer-list>
- How to make bottom border in drawable shape XML selector?
- Create borders on a android view in drawable xml, on 3 sides?
Using a 9-patch
使用 9-patch
You can just make a 9-patch image with a single border. Everything else is the same as discussed above.
您可以制作带有单个边框的 9-patch 图像。其他一切都与上面讨论的相同。
Using a View
使用视图
This is kind of a trick but it works well if you need to add a seperator between two views or a border to a single TextView.
这是一种技巧,但如果您需要在两个视图之间添加分隔符或向单个 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>
Here are some more links:
这里还有一些链接:
回答by Young Gu
The simple way is to add a view for your TextView. Example for the bottom border line:
简单的方法是为您的 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>
For the other direction borders, please adjust the location of the separator view.
对于其他方向边框,请调整分隔符视图的位置。
回答by Bojan Jovanovic
I have solved this issue by extending the textview and drawing a border manually. I even added so you can select if a border is dotted or dashed.
我已经通过扩展 textview 并手动绘制边框解决了这个问题。我什至添加了这样你可以选择边框是虚线还是虚线。
public class BorderedTextView extends TextView {
private Paint paint = new Paint();
public static final int BORDER_TOP = 0x00000001;
public static final int BORDER_RIGHT = 0x00000002;
public static final int BORDER_BOTTOM = 0x00000004;
public static final int BORDER_LEFT = 0x00000008;
private Border[] borders;
public BorderedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public BorderedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BorderedTextView(Context context) {
super(context);
init();
}
private void init(){
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(4);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(borders == null) return;
for(Border border : borders){
paint.setColor(border.getColor());
paint.setStrokeWidth(border.getWidth());
if(border.getStyle() == BORDER_TOP){
canvas.drawLine(0, 0, getWidth(), 0, paint);
} else
if(border.getStyle() == BORDER_RIGHT){
canvas.drawLine(getWidth(), 0, getWidth(), getHeight(), paint);
} else
if(border.getStyle() == BORDER_BOTTOM){
canvas.drawLine(0, getHeight(), getWidth(), getHeight(), paint);
} else
if(border.getStyle() == BORDER_LEFT){
canvas.drawLine(0, 0, 0, getHeight(), paint);
}
}
}
public Border[] getBorders() {
return borders;
}
public void setBorders(Border[] borders) {
this.borders = borders;
}
}
And the border class:
和边界类:
public class Border {
private int orientation;
private int width;
private int color = Color.BLACK;
private int style;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getStyle() {
return style;
}
public void setStyle(int style) {
this.style = style;
}
public int getOrientation() {
return orientation;
}
public void setOrientation(int orientation) {
this.orientation = orientation;
}
public Border(int Style) {
this.style = Style;
}
}
Hope this helps someone :)
希望这对某人有所帮助:)
回答by sdtechcomm
I was just looking at a similar answer-- it's able to be done with a Stroke and the following override:
我只是在看一个类似的答案——它可以通过 Stroke 和以下覆盖来完成:
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Paint strokePaint = new Paint();
strokePaint.setARGB(255, 0, 0, 0);
strokePaint.setTextAlign(Paint.Align.CENTER);
strokePaint.setTextSize(16);
strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(2);
Paint textPaint = new Paint();
textPaint.setARGB(255, 255, 255, 255);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(16);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText("Some Text", 100, 100, strokePaint);
canvas.drawText("Some Text", 100, 100, textPaint);
super.draw(canvas, mapView, shadow);
}
回答by Protean
Simplest solution I've found (and which actually works):
我发现的最简单的解决方案(实际上有效):
<TextView
...
android:background="@android:drawable/editbox_background" />
回答by Maulik Santoki
You can set the border by two methods. One is by drawable and the second is programmatic.
您可以通过两种方法设置边框。一个是可绘制的,第二个是程序化的。
Using 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>
Programmatic
程序化
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;
}
回答by Nick
I found a better way to put a border around a TextView.
我找到了一种在 TextView 周围放置边框的更好方法。
Use a nine-patch image for the background. It's pretty simple, the SDK comes with a tool to make the 9-patch image, and it involves absolutely nocoding.
使用九个补丁图像作为背景。很简单,SDK自带了一个工具来制作9-patch图像,它完全不涉及编码。
The link is http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch.
该链接是http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch。
回答by newbie
You can add something like this in your code:
您可以在代码中添加如下内容:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
回答by Bahaa Hany
Check the link below to make rounded corners http://androidcookbook.com/Recipe.seam?recipeId=2318
检查下面的链接以制作圆角 http://androidcookbook.com/Recipe.seam?recipeId=2318
The drawable folder, under res, in an Android project is not restricted to bitmaps (PNG or JPG files), but it can also hold shapes defined in XML files.
Android 项目中 res 下的 drawable 文件夹不仅限于位图(PNG 或 JPG 文件),还可以保存在 XML 文件中定义的形状。
These shapes can then be reused in the project. A shape can be used to put a border around a layout. This example shows a rectangular border with curved corners. A new file called customborder.xml is created in the drawable folder (in Eclipse use the File menu and select New then File, with the drawable folder selected type in the file name and click Finish).
然后可以在项目中重复使用这些形状。形状可用于在布局周围放置边框。此示例显示带有弯曲角的矩形边框。在 drawable 文件夹中创建一个名为 customborder.xml 的新文件(在 Eclipse 中使用 File 菜单并选择 New 然后选择 File,在文件名中选择 drawable 文件夹并单击 Finish)。
The XML defining the border shape is entered:
输入定义边框形状的 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>
The attribute android:shape
is set to rectangle (shape files also support oval, line, and ring). Rectangle is the default value, so this attribute could be left out if it is a rectangle being defined. See the Android documentation on shapes at http://developer.android.com/guide/topics/resources/drawable-resource.html#Shapefor detailed information on a shape file.
属性android:shape
设置为矩形(形状文件也支持椭圆、线和环)。Rectangle 是默认值,因此如果它是一个正在定义的矩形,则可以省略此属性。有关形状文件的详细信息,请参阅http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape上有关形状的 Android 文档。
The element corners sets the rectangle corners to be rounded. It is possible to set a different radius on each corner (see the Android reference).
元素角将矩形角设置为圆角。可以在每个角上设置不同的半径(请参阅 Android 参考)。
The padding attributes are used to move the contents of the View to which the shape is applied, to prevent the contents overlapping the border.
padding 属性用于移动应用了形状的 View 的内容,以防止内容与边框重叠。
The border color here is set to a light gray (CCCCCC hexadecimal RGB value).
这里的边框颜色设置为浅灰色(CCCCCC 十六进制 RGB 值)。
Shapes also support gradients, but that is not being used here. Again, see the Android resources to see how a gradient is defined. The shape is applied to the laypout using android:background="@drawable/customborder"
.
形状也支持渐变,但这里没有使用。同样,请参阅 Android 资源以了解如何定义渐变。使用 将形状应用于布局android:background="@drawable/customborder"
。
Within the layout other views can be added as normal. In this example, a single TextView has been added, and the text is white (FFFFFF hexadecimal RGB). The background is set to blue, plus some transparency to reduce the brightness (A00000FF hexadecimal alpha RGB value). Finally the layout is offset from the screen edge by placing it into another layout with a small amount of padding. The full layout file is thus:
在布局中,可以正常添加其他视图。在本例中,添加了单个 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>