[英]Adding border to TextView programmatically
我想要做的是在 TextView 周围添加一个边框,而文本视图的中间应该是透明的。 这是我的代码:
LayerDrawable borders = getBorders(
Color.TRANSPARENT, // Background color
Color.parseColor("#CCCCCC"), // Border color
2, // Left border in pixels
2, // Top border in pixels
2, // Right border in pixels
2 // Bottom border in pixels
);
TextView cell = (TextView) super.getView(position, convertView, parent);
cell.setBackground(borders);
// Custom method to generate one or multi side border for a view
protected LayerDrawable getBorders(int bgColor, int borderColor,
int left, int top, int right, int bottom){
// Initialize new color drawables
ColorDrawable borderColorDrawable = new ColorDrawable(borderColor);
ColorDrawable backgroundColorDrawable = new ColorDrawable(bgColor);
// Initialize a new array of drawable objects
Drawable[] drawables = new Drawable[]{
borderColorDrawable,
backgroundColorDrawable
};
// Initialize a new layer drawable instance from drawables array
LayerDrawable layerDrawable = new LayerDrawable(drawables);
// Set padding for background color layer
layerDrawable.setLayerInset(
1, // Index of the drawable to adjust [background color layer]
left, // Number of pixels to add to the left bound [left border]
top, // Number of pixels to add to the top bound [top border]
right, // Number of pixels to add to the right bound [right border]
bottom // Number of pixels to add to the bottom bound [bottom border]
);
// Finally, return the one or more sided bordered background drawable
return layerDrawable;
但是,上面的代码生成了浅灰色的文本视图。 文本视图的中间根本不透明。 我该如何解决?
创建可绘制文件
abc.xml
<?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="#00ffffff" />
<corners android:radius="2dp" />
<stroke android:width="1dp" android:color="#e1e1e1" />
</shape>
</item>
</selector>
并设置背景
cell.setBackground(R.drawable.abc);
您可以更改背景颜色以更改 xml 文件中的<solid android:color="#00ffffff" />
颜色
你可以试试这个。 它将帮助您以编程方式在 textview 上添加边框
GradientDrawable gradientDrawableDefault = new GradientDrawable();
gradientDrawableDefault.setStroke((int) context.getResources().getDimension(R.dimen.dp1), ContextCompat.getColor(context,R.color.color_player_line));
gradientDrawableDefault.setCornerRadius(
context.getResources().getDimension(R.dimen.dp5));
gradientDrawableDefault.setColor(Color.TRANSPARENT);
在textview的背景中设置gradientDrawableDefault。
试试下面的按钮或 textview 或 Edittext 代码.......享受
button.setBackground(getAngleDrawable(backgroundcolor, new float[]{20, 20, 20, 20, 20, 20, 20, 20},strokeWidth,strokeColor));
public static GradientDrawable getAngleDrawable(int solidColor,float[] _radius,int strokeWidth,int strokeColor)
{
GradientDrawable gradientDrawable= new GradientDrawable();
gradientDrawable.setColor(solidColor);
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadii(_radius);
gradientDrawable.setStroke(strokeWidth, strokeColor);
return gradientDrawable;
}
您可以使用自己的形状:
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.