如何在 textView 中将圆形绘制为背景而不是方形背景?
How to paint a circle as background in a textView instead of square background?
我正在尝试动态绘制一个圆圈作为 textView 的背景,但它绘制的不是整个方形背景
我试过一种有效的静态方法:
这是我的代码
circle_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<corners android:radius="4dip" />
<stroke
android:width="5dip"
android:color="@color/colorPrimary" />
<solid android:color="@color/colorPrimary" />
</shape>
list_item.xml
<TextView
android:id="@+id/tv_tag"
android:layout_width="50dp"
android:layout_height="54dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:background="@drawable/circle_drawable"
android:gravity="center"
android:text="B"
android:textColor="#fff"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.454" />
静态工作于:
TextView tv_tag = (TextView)v.findViewById(R.id.tv_tag);
tv_tag.setText(itemTag);
但是当我尝试使用 setBackgroundColor
更改背景颜色时它不起作用
TextView tv_tag = (TextView)v.findViewById(R.id.tv_tag);
tv_tag.setText(itemTag);
tv_tag.setBackgroundColor(Color.parseColor("#D81B60"));
任何帮助对我来说都是很棒的。
setBackgroundColor
不会使用可绘制的形状。它只会设置颜色,而不是您预期的形状。所以改为使用 setBackground
like-
tv_tag.setBackground(ContextCompat.getDrawable(context,R.drawable.circlw_drawable))
TextView tv_tag = (TextView)v.findViewById(R.id.tv_tag);
tv_tag.setText(itemTag);
tv_tag.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.circle_drawable));
这样试试,希望对你有帮助。
GradientDrawable bg = (GradientDrawable)tv_tag.getBackground();
bg.setColor(Color.parseColor("#D81B60"));
使用setBackgroundColor()
方法,我们改变了背景颜色,但颜色设置为textView的背景,默认情况下android中任何视图的背景形状都是矩形。
要设置特定的背景形状,我们可以使用 drawable。
在您的代码中,由于文本视图的矩形形状,您更改了文本视图的背景颜色。
要更改背景颜色,您需要更改可绘制对象的颜色。
请尝试使用以下代码更改可绘制对象的颜色:
将此代码添加到您的 activity
TextView tv_tag=findViewById(R.id.tv_tag);
Drawable mDrawable = getResources().getDrawable(R.drawable.circle_drawable);
mDrawable.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SCREEN));
tv_tag.setBackground(mDrawable);
您可以像这样简单地修改它
GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);
我正在尝试动态绘制一个圆圈作为 textView 的背景,但它绘制的不是整个方形背景
我试过一种有效的静态方法: 这是我的代码
circle_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<corners android:radius="4dip" />
<stroke
android:width="5dip"
android:color="@color/colorPrimary" />
<solid android:color="@color/colorPrimary" />
</shape>
list_item.xml
<TextView
android:id="@+id/tv_tag"
android:layout_width="50dp"
android:layout_height="54dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:background="@drawable/circle_drawable"
android:gravity="center"
android:text="B"
android:textColor="#fff"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.454" />
静态工作于:
TextView tv_tag = (TextView)v.findViewById(R.id.tv_tag);
tv_tag.setText(itemTag);
但是当我尝试使用 setBackgroundColor
更改背景颜色时它不起作用
TextView tv_tag = (TextView)v.findViewById(R.id.tv_tag);
tv_tag.setText(itemTag);
tv_tag.setBackgroundColor(Color.parseColor("#D81B60"));
任何帮助对我来说都是很棒的。
setBackgroundColor
不会使用可绘制的形状。它只会设置颜色,而不是您预期的形状。所以改为使用 setBackground
like-
tv_tag.setBackground(ContextCompat.getDrawable(context,R.drawable.circlw_drawable))
TextView tv_tag = (TextView)v.findViewById(R.id.tv_tag);
tv_tag.setText(itemTag);
tv_tag.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.circle_drawable));
这样试试,希望对你有帮助。
GradientDrawable bg = (GradientDrawable)tv_tag.getBackground();
bg.setColor(Color.parseColor("#D81B60"));
使用setBackgroundColor()
方法,我们改变了背景颜色,但颜色设置为textView的背景,默认情况下android中任何视图的背景形状都是矩形。
要设置特定的背景形状,我们可以使用 drawable。
在您的代码中,由于文本视图的矩形形状,您更改了文本视图的背景颜色。
要更改背景颜色,您需要更改可绘制对象的颜色。
请尝试使用以下代码更改可绘制对象的颜色:
将此代码添加到您的 activity
TextView tv_tag=findViewById(R.id.tv_tag);
Drawable mDrawable = getResources().getDrawable(R.drawable.circle_drawable);
mDrawable.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SCREEN));
tv_tag.setBackground(mDrawable);
您可以像这样简单地修改它
GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);