如何在 Android TextView 周围添加边框?
是否可以在 Android TextView
周围绘制边框?
Is it possible to draw a border around an Android TextView
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以在 Android TextView
周围绘制边框?
Is it possible to draw a border around an Android TextView
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(23)
您可以将可绘制的形状(矩形)设置为视图的背景。
和矩形drawable back.xml(放入res/drawable文件夹中):
您可以使用
@android:color/transparent
使纯色具有透明背景。您还可以使用填充将文本与边框分开。
有关详细信息,请参阅:http://developer.android.com/guide/主题/资源/drawable-resource.html
You can set a shape drawable (a rectangle) as background for the view.
And rectangle drawable back.xml (put into res/drawable folder):
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
让我总结一下几种不同的(非编程)方法。
使用形状可绘制
将以下内容保存为可绘制文件夹中的 XML 文件(例如 my_border.xml):
然后将其设置为 TextView 的背景:
更多帮助:
使用 9-patch
9-patch 是一个可拉伸的背景图像。如果你制作一个带有边框的图像,那么它会给你的 TextView 一个边框。您需要做的就是制作图像,然后将其设置为 TextView 中的背景。
以下是一些展示如何制作 9-patch 图像的链接:
如果我只想要顶部边框怎么办?
使用图层列表
您可以使用图层列表将两个矩形堆叠在一起。通过使第二个矩形比第一个矩形稍小,您可以制作边框效果。第一个(下方)矩形是边框颜色,第二个矩形是背景颜色。
设置
android:top="2dp"
会将顶部偏移(使其变小)2dp。这允许第一个(下部)矩形显示出来,产生边框效果。您可以将其应用到 TextView 背景,就像上面完成shape
可绘制对象一样。以下是有关图层列表的更多链接:
使用 9 块
您可以只制作带有单个边框的 9 块图像。其他一切都与上面讨论的相同。
使用视图
这是一种技巧,但如果您需要在两个视图之间添加分隔符或向单个 TextView 添加边框,那么它会很有效。
以下是更多链接:
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):
Then just set it as the background to your TextView:
More help:
Using a 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.
Here are some links that will show how to make a 9-patch image:
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.
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 theshape
drawable was done above.Here are some more links about layer lists:
Using a 9-patch
You can just make a 9-patch image with a single border. Everything else is the same as discussed above.
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.
Here are some more links:
简单的方法是为 TextView 添加一个视图。底部边框线示例:
对于其他方向边框,请调整分隔视图的位置。
The simple way is to add a view for your TextView. Example for the bottom border line:
For the other direction borders, please adjust the location of the separator view.
我通过扩展文本视图并手动绘制边框解决了这个问题。
我什至添加了这样您可以选择边框是点线还是虚线。
和边界类:
希望这对某人有帮助:)
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.
And the border class:
Hope this helps someone :)
我发现的最简单的解决方案(并且实际上有效):
Simplest solution I've found (and which actually works):
您可以通过两种方法设置边框。一种是通过可绘制的,第二种是通过编程的。
使用 Drawable
编程
You can set the border by two methods. One is by drawable and the second is programmatic.
Using Drawable
Programmatic
通过材质组件库,您可以使用
MaterialShapeDrawable
。然后,您可以以编程方式应用
MaterialShapeDrawable
:With the Material Components Library you can use the
MaterialShapeDrawable
.Then you can programmatically apply a
MaterialShapeDrawable
:我只是在看一个类似的答案——它可以通过 Stroke 和以下覆盖来完成:
I was just looking at a similar answer-- it's able to be done with a Stroke and the following override:
您可以在代码中添加类似这样的内容:
You can add something like this in your code:
您可以将可绘制的形状(带角的矩形)设置为视图的背景。
和矩形可绘制frame.xml(放入res/drawable文件夹中):
You can set a shape drawable (a rectangle with corners) as background for the view.
And rectangle drawable frame.xml (put into res/drawable folder):
我找到了一种更好的方法来在 TextView 周围添加边框。
使用九块图像作为背景。非常简单,SDK 附带了一个制作 9 补丁图像的工具,并且完全不涉及编码。
链接是 http://developer.android.com /guide/topics/graphics/2d-graphics.html#nine-patch。
I found a better way to put a border around a 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 no coding.
The link is http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch.
检查下面的链接以制作圆角
http://androidcookbook.com/Recipe.seam?recipeId=2318
可绘制文件夹Android 项目中的 res 下的 不限于位图(PNG 或 JPG 文件),还可以保存 XML 文件中定义的形状。
然后可以在项目中重复使用这些形状。形状可用于在布局周围放置边框。此示例显示了带有弯角的矩形边框。将在可绘制文件夹中创建一个名为 customborder.xml 的新文件(在 Eclipse 中,使用“文件”菜单并选择“新建”,然后选择“文件”,在选定的可绘制文件夹中输入文件名,然后单击“完成”)。
输入定义边框形状的 XML:
属性
android:shape
设置为矩形(形状文件还支持椭圆形、直线和环形)。矩形是默认值,因此如果定义的是矩形,则可以省略此属性。请参阅有关形状的 Android 文档 http://developer.android .com/guide/topics/resources/drawable-resource.html#Shape 了解有关形状文件的详细信息。元素角将矩形角设置为圆角。可以在每个角上设置不同的半径(请参阅 Android 参考)。
padding 属性用于移动应用形状的视图的内容,以防止内容与形状重叠。
边界。
这里的边框颜色设置为浅灰色(CCCCCC 十六进制 RGB 值)。
形状也支持渐变,但这里没有使用渐变。再次,请参阅 Android 资源以了解渐变是如何定义的。使用
android:background="@drawable/customborder"
将形状应用于布局。在布局中可以照常添加其他视图。在此示例中,添加了单个 TextView,文本为白色(FFFFFF 十六进制 RGB)。背景设置为蓝色,加上一些透明度以降低亮度(A00000FF 十六进制 alpha RGB 值)。最后,通过将布局放置到具有少量填充的另一个布局中,使布局从屏幕边缘偏移。完整的布局文件如下:
Check the link below to make rounded corners
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.
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).
The XML defining the border shape is entered:
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#Shape for detailed information on a shape file.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).
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.
The border color here is set to a light gray (CCCCCC hexadecimal RGB value).
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"
.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 进行平方时,我只需将它们放入 LinearLayout 中即可。我设置了 LinearLayout 的背景颜色,并为 TextView 添加了边距。结果就像平方 TextView 一样。
I have a way to do it very simply, and I'd like to share it.
When I want to square mi TextViews, I just put them in a LinearLayout. I set the background color of my LinearLayout, and I add a margin to my TextView. The result is exactly as if you square the TextView.
您可以为文本视图创建自定义背景。
步骤
对于您想要将其用作背景的文本视图,
android:background="@drawable/your_fileName"
You can create custom background for your text view.
Steps
For your text view where you want to use it as backgroud,
android:background="@drawable/your_fileName"
更改 Konstantin Burov 答案,因为在我的情况下不起作用:
compileSdkVersion 26 (Android 8.0),
minSdkVersion 21(Android 5.0),
targetSdkVersion 26,
实现 'com.android.support:appcompat-v7:26.1.0',
等级:4.1
Changing Konstantin Burov answer because not work in my case:
compileSdkVersion 26 (Android 8.0),
minSdkVersion 21 (Android 5.0),
targetSdkVersion 26,
implementation 'com.android.support:appcompat-v7:26.1.0',
gradle:4.1
这是我的“简单”辅助类,它返回带边框的 ImageView。只需将其放入您的 utils 文件夹中,然后像这样调用它:
这是代码。
Here is my 'simple' helper class which returns an ImageView with the border. Just drop this in your utils folder, and call it like this:
Here is the code.
有很多方法可以向 textView 添加边框。最简单的方法是创建一个自定义可绘制对象并将其设置为 textView 的
android:background="@drawable/textview_bg"
。textview_bg.xml 将位于 Drawables 下,可以是这样的。
您可以使用
实心
或渐变
背景(如果不需要,则什么都没有),角
来添加角半径和描边< /code> 添加边框。
textview_bg.xml
There are a lot of ways to add a border to a textView. The simplest one is by creating a custom drawable and setting it as
android:background="@drawable/textview_bg"
for your textView.The textview_bg.xml will go under Drawables and can be something like this.
You can have a
solid
or agradient
background (or nothing if not required),corners
to add a corner radius andstroke
to add border.textview_bg.xml
这可能对你有帮助。
This may help you.
创建一个边框视图,其背景颜色为边框颜色,文本视图大小为文本视图。将边框视图填充设置为边框的宽度。将文本视图背景颜色设置为文本视图所需的颜色。现在将文本视图添加到边框视图内。
Create a border view with the background color as the color of the border and size of your text view. set border view padding as the width of the border. Set text view background color as the color you want for the text view. Now add your text view inside the border view.
试试这个:
Try this:
这段代码足够你可以放在任何你想要的地方
this code enough you can place wherever you want
在您的 xml 文本视图上设置背景,
将 rounded_textview.xml 文件添加到您的可绘制目录中。
在textView背景中设置可绘制文件。
setBackground on your xml textview,
add rounded_textview.xml file into your drawable directory.
set drawable file in textView background.
其实很简单。如果您想要在 Textview 后面有一个简单的黑色矩形,只需在 TextView 标记内添加
android:background="@android:color/black"
即可。像这样:Actually, it is very simple. If you want a simple black rectangle behind the Textview, just add
android:background="@android:color/black"
within the TextView tags. Like this: