我想在运行时将所有边的边框添加到RelativeLayout
。我能够获得边框,但它正在改变布局的背景颜色。如果我将背景颜色设置为white
,它也会将边框颜色更改为白色。我该如何避免?
以下是我的代码:
private static void drawBorder(Context context, View layout){
final RectShape rect = new RectShape();
final ShapeDrawable rectShapeDrawable = new ShapeDrawable(rect);
final Resources res = context.getResources();
final Paint paint = rectShapeDrawable.getPaint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLUE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10);
layout.setBackgroundDrawable(rectShapeDrawable); //Testing on phone with API < 16
}
感谢。
答案 0 :(得分:0)
只需创建一个带有以下边框描述的xml文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="2dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp" />
<stroke
android:width="1dp"
android:color="@android:color/white" />
</shape>
并使用此drawable
为视图设置backgroundresource答案 1 :(得分:0)
您可以使用:
private static Bitmap drawBitmapWithBorders(int width, int height, int borderWidth,
int backgroundColor, int borderColor){
int pixels[] = new int[width*height];
Arrays.fill(pixels, borderColor);
for(int i=borderWidth;i<height-borderWidth;i++){
Arrays.fill(pixels, i*width+borderWidth, (i+1)*width-borderWidth, backgroundColor);
}
return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}