是否可以在Android TextView周围绘制边框?
当前回答
检查下面的链接以制作圆角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>
其他回答
检查下面的链接以制作圆角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>
简单的方法是为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>
对于其他方向的边框,请调整分隔符视图的位置。
这是我的“简单”助手类,它返回带有边框的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;
}
}
我找到了一种更好的方法来在TextView周围添加边框。
使用九块图像作为背景。它非常简单,SDK附带了一个制作9补丁图像的工具,而且它绝对不需要编码。
链接是http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-补丁。
我只是在寻找一个类似的答案——它可以通过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);
}
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置