更改android textview边框颜色而不更改背景 [英] Change android textview border color without changing background

查看:173
本文介绍了更改android textview边框颜色而不更改背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据某些条件,我正在动态设置textview的背景

I am setting background of a textview dynamically, based on some conditions as

textview.setBackgroundResource(R.drawable.attempted_question_border);

OR

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"和边框色"skipped_question_background".到目前为止,一切都很好.现在,我需要仅更改此文本视图的边框颜色保持背景颜色不变.我尝试使用下面的xml更改背景.

It sets background color 'answered_que_bg' and border color 'answered_que_bg' OR background color 'skipped_question_background' and border color 'skipped_question_background'. So far so good. Now I need to change only border color of this textview keeping background color same as it had. I tried with changing background with below 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>

它会根据需要更改边框颜色,但背景颜色也会丢失.

It changes border color as desired but background color is also lost.

推荐答案

使用具有这样的图层列表的可绘制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);

您的代码没有更改笔触的颜色

With your code without change color of stroke

使用我的代码"可以更改笔触的颜色,也可以在注释代码中以编程方式更改背景

With My code change color of stroke and also you can change the background progrmatically in comment code

这篇关于更改android textview边框颜色而不更改背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆