以编程方式向TextView添加边框

问题描述 投票:1回答:3

我想做的是在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;

但是,上面的代码产生了浅灰色的文本视图。文本视图的中间一点都不透明。我该如何解决?

java android textview border transparent
3个回答
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
投票

您可以尝试这个。它将帮助您以编程方式在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。


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);
© www.soinside.com 2019 - 2024. All rights reserved.