Я пытаюсь добавить рамку вокруг 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;
Однако приведенный выше код создал текстовое представление светло-серого цвета. Середина текстового представления вообще непрозрачна. Как это исправить?
@pskink Понятно, понятно! Большое спасибо за указание на это!




Создать файл с возможностью рисования
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);
и вы можете изменить цвет фона, чтобы изменить цвет <solid android:color = "#00ffffff" /> в файле xml
Есть ли способ установить непрозрачность цвета границы с помощью файла drawable.xml выше?
@ guest176969 да
Как этого добиться?
выберите цвет из палитры цветов и установите непрозрачность (например, увеличение или уменьшение) в палитре
Вы можете попробовать это. Это поможет вам программно добавить границу в текстовое представление.
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);
установить gradientDrawable Default в фоновом режиме текстового просмотра.
Я вижу, я вижу! Большое спасибо за помощь!
Попробуйте следующий код для кнопки, текстового представления или 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);
это потому, что вы используете два
ColorDrawableодин поверх другого - используйтеGradientDrawableвместоLayerDrawable