是否可以在Android 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的水平线?
其他回答
您可以在代码中添加以下内容:
<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>
有很多方法可以向textView添加边框。最简单的方法是创建一个自定义的可绘制文件,并将其设置为android:background=“@drawable/textview_bg”用于textview。
textview_bg.xml将位于Drawables下,可以是这样的。您可以有一个实心或渐变背景(如果不需要的话,也可以没有背景)、添加角半径的角和添加边框的笔划。
textview_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="@dimen/dp_10"/>
<gradient
android:angle="225"
android:endColor="#FFFFFF"
android:startColor="#E0E0E0" />
<stroke
android:width="2dp"
android:color="#000000"/>
</shape>
可以将可绘制的形状(矩形)设置为视图的背景。
<TextView android:text="Some text" android:background="@drawable/back"/>
和矩形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>
您可以使用@android:color/transparent作为纯色,使其具有透明的背景。您还可以使用填充将文本与边框分开。有关详细信息,请参阅:http://developer.android.com/guide/topics/resources/drawable-resource.html
让我总结一下几种不同的(非编程的)方法。
使用可绘制的形状
将以下内容另存为可绘制文件夹中的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的水平线?
我找到的最简单的解决方案(而且实际可行):
<TextView
...
android:background="@android:drawable/editbox_background" />
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置