是否可以在Android TextView周围绘制边框?
当前回答
事实上,这很简单。如果您想要在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>
我通过扩展文本视图并手动绘制边框解决了这个问题。我甚至添加了这样的选项,您可以选择边框是虚线还是虚线。
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;
}
}
边框类:
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;
}
}
希望这对某人有所帮助:)
更改康斯坦丁·布罗夫的答案,因为在我的情况下不起作用:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<stroke android:width="2dip" android:color="#4fa5d5"/>
<corners android:radius="7dp"/>
</shape>
</item>
</selector>
compileSdkVersion 26(Android 8.0),minSdkVersion 21(Android 5.0),targetSdkV版本26,实现“com.android.support:appcompat-v7:26.1.0”,等级:4.1
您可以为文本视图创建自定义背景。步骤
转到您的项目。转到资源,右键单击可绘制。单击新建->可绘制资源文件为您的文件命名在文件中粘贴以下代码
<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”
让我总结一下几种不同的(非编程的)方法。
使用可绘制的形状
将以下内容另存为可绘制文件夹中的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的水平线?
推荐文章
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?