首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改android文本视图边框颜色而不更改背景

问更改android文本视图边框颜色而不更改背景
EN

Stack Overflow用户
提问于 2016-09-14 10:56:42
回答 2查看 4.8K关注 0票数 1

我根据以下条件动态地设置文本视图的背景

代码语言:javascript
复制
textview.setBackgroundResource(R.drawable.attempted_question_border);

或

代码语言:javascript
复制
textview.setBackgroundResource(R.drawable.skipped_question_background);

背景的Xml是

代码语言:javascript
复制
<?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更改背景。

代码语言:javascript
复制
<?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>

它会根据需要改变边框颜色,但是背景色也会丢失。

EN

回答 2

Stack Overflow用户

发布于 2016-09-14 11:36:01

使用可绘制的xml和图层列表,如。

代码语言:javascript
复制
<?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>

代码以语法方式更改笔画和背景。

代码语言:javascript
复制
      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);

不改变笔画颜色的代码

​

​

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

​

​

票数 2
EN

Stack Overflow用户

发布于 2016-09-14 11:04:07

我将textview放入一个LinearLayout中,在该LinearLayout中,TextView将以两边的某些边距为中心,然后我将更改LinearLayout的背景色,而不改变文本视图的背景色,然后看起来它的边框颜色发生了变化。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39488701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档