我想做的是在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;
但是,上面的代码产生的文本视图为浅灰色。文本视图的中间一点都不透明。有关如何解决此问题的任何想法?
谢谢!
答案 0 :(得分:2)
您可以尝试一下。它将帮助您以编程方式在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。
答案 1 :(得分:0)
创建可绘制文件
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" />
颜色
答案 2 :(得分:0)
尝试下面的代码来实现按钮或文本视图或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);