改变目前Android TextView的边框颜色没有变化的背景

问题描述:

我动态设置一个TextView的背景下,基于一些条件改变目前Android TextView的边框颜色没有变化的背景

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> 

它设置背景颜色'answers_que_bg'和边框颜色'answers_que_bg'或背景颜色's kipped_question_background'和边框颜色'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> 

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

+0

使用'Drawable#setColorFilter' – pskink

+0

@Ram Mehar Deswal你需要其他的d如果你还改变了textview的背景而不是检查评论代码 –

+0

你试过我的回答@林 –

我已经通过将TextView放入一个LinearLayout中,其中TextView将以某边距为中心,然后我将更改LinearLayout的背景颜色,而不更改textview的背景颜色,然后它会显示为边框颜色已经改变。

把你绘制的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); 

与您的代码没有行程

enter image description here

随着我国脑卒中的代码更改颜色的变化颜色,你也可以在注释代码progrmatically改变背景

enter image description here

+0

它可以作为魅力。谢谢 !! –

+0

@RamMeharDeswal欢迎您也接受答案... –