如何使文本视图型圈,设置不同的背景颜色根据病情 [英] How to make Text View shape circle and set different background color based on condition

查看:176
本文介绍了如何使文本视图型圈,设置不同的背景颜色根据病情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的TextView ,我想是让的TextView 形状圆,然后设置不同势背景颜色根据不同的病情,我都习惯了。我能够设置根据不同势条件,背景颜色,但不能使的TextView 形圈。那么,如何可以做。请帮我解决了这一点。

code我用的是

  TextView的txt_stage_display =(TextView中)findViewById(R.id.txt_stage_display);

        如果(m_enStage [位置] == enSTAGE_START)
        {
            txt_stage_display.setBackgroundColor(Color.parseColor(#D48393));
        }

        否则,如果(m_enStage [位置] == enSTAGE_FLOW)
        {

            txt_stage_display.setBackgroundColor(Color.parseColor(#D48393));
        }
        否则,如果(m_enStage [位置] == enSTAGE_SAFE)
        {

            txt_stage_display.setBackgroundColor(Color.parseColor(#66B0CC));
        }
        否则,如果(m_enStage [位置] == enSTAGE_UNSAFE)
        {
            txt_stage_display.setBackgroundColor(Color.parseColor(#D8C627));
        }
        否则,如果(m_enStage [位置] == enSTAGE_FERTILE)
        {
            txt_stage_display.setBackgroundColor(Color.parseColor(#67A05E));
        }
        其他
        {

            txt_stage_display.setBackgroundColor(Color.parseColor(#808080));
        }
 

解决方案

如果你有一个相对小量的颜色,你可以创建一个文件,绘制每种颜色,例如创建一个文件bg_red.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<项目的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
  <形状>
      [固体机器人:颜色=#F00/>
      <角落
          机器人:topLeftRadius =30dp
          机器人:topRightRadius =30dp
          机器人:bottomLeftRadius =30dp
          机器人:bottomRightRadius =30dp
          />
  < /形状>
< /项目>
 

然后分配定义TextView的:

 <的TextView
    机器人:ID =@ + ID /电视
    机器人:layout_height =60dp
    机器人:layout_width =60dp
    机器人:文本=X
    机器人:文字颜色=#FFF
    机器人:TEXTSIZE =20SP
    机器人:背景=@可绘制/ bg_red
    机器人:重力=center_vertical | center_horizo​​ntal
    />
 

请注意,该宽度两倍于背景拐角半径的半径。

要由code改变颜色:

  TextView的V =(TextView中)findViewById(R.id.my_text_view);
v.setBackgroundResource(R.drawable.bg_blue);
 

I have an TextView, what i want is to make the TextView shape circle and then set diffrent background color based on different condition i have used. I am able to set Background color based on diffrent conditions but not able to make the TextView shape circle. So how that can be done. Please help me to solve this out.

Code i have used is

        TextView txt_stage_display   = (TextView)findViewById(R.id.txt_stage_display);

        if(m_enStage[position] == enSTAGE_START)
        {
            txt_stage_display.setBackgroundColor(Color.parseColor("#D48393"));              
        }

        else if(m_enStage[position] == enSTAGE_FLOW)
        {

            txt_stage_display.setBackgroundColor(Color.parseColor("#D48393"));              
        }
        else if(m_enStage[position] == enSTAGE_SAFE)
        {

            txt_stage_display.setBackgroundColor(Color.parseColor("#66B0CC"));              
        }
        else if(m_enStage[position] == enSTAGE_UNSAFE)
        {
            txt_stage_display.setBackgroundColor(Color.parseColor("#D8C627"));              
        }
        else if(m_enStage[position] == enSTAGE_FERTILE)
        {
            txt_stage_display.setBackgroundColor(Color.parseColor("#67A05E"));                                  
        }
        else
        {

            txt_stage_display.setBackgroundColor(Color.parseColor("#808080"));              
        }

解决方案

If you have a relatively small amout of colors, you can create a drawable file for each color, for example create a file bg_red.xml:

<?xml version="1.0" encoding="utf-8"?>
<item xmlns:android="http://schemas.android.com/apk/res/android">
  <shape>
      <solid android:color="#f00" />
      <corners
          android:topLeftRadius="30dp"
          android:topRightRadius="30dp"
          android:bottomLeftRadius="30dp"
          android:bottomRightRadius="30dp"
          />
  </shape>
</item>

Then assign define the TextView:

<TextView 
    android:id="@+id/tv"
    android:layout_height="60dp"
    android:layout_width="60dp" 
    android:text="X" 
    android:textColor="#fff"
    android:textSize="20sp"
    android:background="@drawable/bg_red"
    android:gravity="center_vertical|center_horizontal" 
    />

Note that the width is twice the radius of the background corner radius.

To change the color from code:

TextView v = (TextView) findViewById(R.id.my_text_view);
v.setBackgroundResource(R.drawable.bg_blue);

这篇关于如何使文本视图型圈,设置不同的背景颜色根据病情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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