Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式向TextView添加边框_Java_Android_Textview_Border_Transparent - Fatal编程技术网

Java 以编程方式向TextView添加边框

Java 以编程方式向TextView添加边框,java,android,textview,border,transparent,Java,Android,Textview,Border,Transparent,我想做的是在文本视图周围添加一个边框,而文本视图的中间应该是透明的。这是我的密码: 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

我想做的是在文本视图周围添加一个边框,而文本视图的中间应该是透明的。这是我的密码:

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>
您可以更改背景颜色,以更改xml文件中的
颜色

创建可绘制文件

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>

您可以更改背景颜色来更改xml文件中的
颜色

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

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

请尝试以下按钮、文本视图或编辑文本的代码……….尽情享受吧

 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);

请尝试以下按钮、文本视图或编辑文本的代码……….尽情享受吧

 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);

这是因为您使用的是两个
ColorDrawable
s,一个在另一个之上-使用
GradientDrawable
而不是
LayerDrawable
@pskink我明白了我明白了!非常感谢你指出这一点!这是因为您使用的是两个
ColorDrawable
s,一个在另一个之上-使用
GradientDrawable
而不是
LayerDrawable
@pskink我明白了我明白了!非常感谢你指出这一点!有没有办法使用上面的drawable.xml设置边框颜色的不透明度?@guest176969 yesHow来实现它?从颜色选择器中选择颜色并设置不透明度(如增加或减少)从pickerIs有没有办法使用上面的drawable.xml设置边框颜色的不透明度?@guest176969 yesHow来实现它?从颜色选择器中选择颜色,从pickerI设置不透明度(如增加或减少),我看到了!非常感谢你的帮助!我明白了,我明白了!非常感谢你的帮助!