是否可以在文本视图周围画一个边框?
你可以设置一个可画的形状(一个矩形)作为视图的背景。
<TextView android:text="Some text" android:background="@drawable/back"/>
和矩形可画的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
来表示纯色,以便有一个透明的背景。
你也可以使用padding将文本与边界分开。
更多信息见: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" />
更多帮助。
使用9个贴片
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" />
以下是一些链接,将告诉大家如何制作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背景上,就像上面的 "形状 "可绘制一样。
这里有一些关于图层列表的链接。
使用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>
这里还有一些链接。
[1]: http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape [2]: http://android-dev-tips-and-tricks.blogspot.com/2012/08/xml-drawables-part-i.html [3]: http://developer.android.com/tools/help/draw9patch.html [4]: http://romannurik.github.io/AndroidAssetStudio/nine-patches.html [5]: http://radleymarx.com/blog/simple-guide-to-9-patch/ [6]: https://stackoverflow.com/a/27633481/3681880
简单的方法是为你的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>
其他方向的边框,请调整分离器视图的位置。
我已经通过扩展文本视图和手动绘制边框解决了这个问题。 我甚至还添加了可以选择边框是点还是虚线的功能。
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;
}
}
希望能帮到大家:)
我只是在看一个类似的答案--它能够用一个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);
}
你可以通过两种方法设置边框。 一种是通过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;
}
请看下面的链接,让圆角变得更好 http://androidcookbook.com/Recipe.seam?recipeId=2318
安卓项目中res下的可绘制文件夹并不局限于位图(PNG或JPG文件),它还可以保存在XML文件中定义的形状。
这些形状可以在项目中重复使用。 形状可以用来在布局周围设置一个边框。 这个例子显示的是一个带有弧形角的矩形边框。 在drawable文件夹中创建一个名为customborder.xml的新文件(在Eclipse中使用File菜单,选择New然后选择File,选择drawable文件夹后键入文件名,然后单击Finish)。
输入定义边框形状的 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。
元素corners设置矩形的角是圆角。 可以对每个角设置不同的半径(参见Android参考资料)。
padding属性用于移动应用形状的View的内容,以防止内容重叠在 边框。
这里的边框颜色设置为浅灰色(CCCCCC十六进制RGB值)。
形状也支持渐变,但这里不使用。
同样,请参阅Android资源来了解如何定义渐变。
使用android:background="@drawable/customborder"
将形状应用到布局中。
在布局中,可以像正常情况下一样添加其他视图。 在这个例子中,我们添加了一个TextView,文本是白色的(FFBFF十六进制RGB)。 背景设置为蓝色,加上一些透明度以降低亮度(A00000FF十六进制alpha RGB值)。 最后将布局与屏幕边缘偏移,将其放入另一个布局中,并加上少量的padding。 因此,完整的布局文件是这样的:{{7385208}}。
<?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>
我有一个方法可以很简单地做到这一点,我'想分享一下。
当我想把Mi TextViews方块化时,我只需要把它们放在一个LinearLayout中。 我设置了LinearLayout的背景色,并为我的TextView添加了一个边距。 结果就和你把TextView平方化一样。
这是我的'simple' 助手类,它将返回一个带有边框的ImageView。 只要把它放到你的utils文件夹中,然后像这样调用它。
ImageView selectionBorder = BorderDrawer.generateBorderImageView(context, borderWidth, borderHeight, thickness, Color.Blue);
这里是代码。
/**
* Because creating a border is Rocket Science in Android.
*/
public class BorderDrawer
{
public static ImageView generateBorderImageView(Context context, int borderWidth, int borderHeight, int borderThickness, int color)
{
ImageView mask = new ImageView(context);
// Create the square to serve as the mask
Bitmap squareMask = Bitmap.createBitmap(borderWidth - (borderThickness*2), borderHeight - (borderThickness*2), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(squareMask);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(color);
canvas.drawRect(0.0f, 0.0f, (float)borderWidth, (float)borderHeight, paint);
// Create the darkness bitmap
Bitmap solidColor = Bitmap.createBitmap(borderWidth, borderHeight, Bitmap.Config.ARGB_8888);
canvas = new Canvas(solidColor);
paint.setStyle(Paint.Style.FILL);
paint.setColor(color);
canvas.drawRect(0.0f, 0.0f, borderWidth, borderHeight, paint);
// Create the masked version of the darknessView
Bitmap borderBitmap = Bitmap.createBitmap(borderWidth, borderHeight, Bitmap.Config.ARGB_8888);
canvas = new Canvas(borderBitmap);
Paint clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawBitmap(solidColor, 0, 0, null);
canvas.drawBitmap(squareMask, borderThickness, borderThickness, clearPaint);
clearPaint.setXfermode(null);
ImageView borderView = new ImageView(context);
borderView.setImageBitmap(borderBitmap);
return borderView;
}
}
改变Konstantin Burov的答案,因为在我的情况下没有工作。
<?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>
compileSdkV版本26(Android 8.0)。 minSdkV版本21(Android 5.0)。 targetSdkVersion 26, 实现'com.android.support:appcompat-v7:26.1.0'。 gradle:4.1
这可能对你有帮助。
<RelativeLayout
android:id="@+id/textbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@android:color/darker_gray" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="3dp"
android:background="@android:color/white"
android:gravity="center"
android:text="@string/app_name"
android:textSize="20dp" />
</RelativeLayout
有很多方法可以为textView添加一个边框,最简单的方法是创建一个自定义的drawable,并将其设置为android:background="@drawable/textview_bg"
为你的textView添加。
最简单的方法是创建一个自定义的drawable,并将其设置为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:background="@android:color/black"
就可以了。
如果你想在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"/>