我根据以下条件动态地设置文本视图的背景
textview.setBackgroundResource(R.drawable.attempted_question_border);或
textview.setBackgroundResource(R.drawable.skipped_question_background);背景的Xml是
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="@color/answered_que_bg" >
</solid>
<stroke
android:width="1dp"
android:color="@color/answered_que_bg" >
</stroke>
<corners
android:radius="2dp">
</corners>
</shape>它设置背景颜色'answered_que_bg‘和边框颜色'answered_que_bg’或背景颜色'skipped_question_background‘和边框颜色’跳过_问题_背景‘。到目前一切尚好。现在我需要只更改文本视图的边框颜色,保持背景色与原来的相同。我尝试用下面的xml更改背景。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="@color/bookmark_color" >
</stroke>
<corners
android:radius="2dp">
</corners>
</shape>它会根据需要改变边框颜色,但是背景色也会丢失。
发布于 2016-09-14 11:36:01
使用可绘制的xml和图层列表,如。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/shape">
<shape
android:shape="rectangle" >
<solid
android:color="@color/colorPrimary" >
</solid>
<stroke
android:width="1dp"
android:color="@color/colorPrimary" >
</stroke>
<corners
android:radius="2dp">
</corners>
</shape>
</item>
</layer-list>代码以语法方式更改笔画和背景。
LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(demo.this,R.drawable.drawtext);
GradientDrawable gradientDrawable = (GradientDrawable) shape.findDrawableByLayerId(R.id.shape);
// if you want to change the color of background of textview dynamically
//gradientDrawable.setColor(ContextCompat.getColor(demo.this,R.color.colorAccent));
// This is mangage the storke width and it's color by this shape.setStroke(strokeWidth,color);
gradientDrawable.setStroke(2,ContextCompat.getColor(demo.this,R.color.colorAccent));
text2.setBackground(shape);不改变笔画颜色的代码

使用我的代码更改笔画的颜色,也可以在注释代码中提前更改背景。

发布于 2016-09-14 11:04:07
我将textview放入一个LinearLayout中,在该LinearLayout中,TextView将以两边的某些边距为中心,然后我将更改LinearLayout的背景色,而不改变文本视图的背景色,然后看起来它的边框颜色发生了变化。
https://stackoverflow.com/questions/39488701
复制相似问题