Android 如何制作文本视图形状的圆圈并根据条件设置不同的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10316354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:16:02  来源:igfitidea点击:

How to make Text View shape circle and set different background color based on condition

android

提问by AndroidDev

I have an TextView, what i want is to make the TextViewshape 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 TextViewshape circle. So how that can be done. Please help me to solve this out.

我有一个TextView,我想要的是制作TextView圆形,然后根据我使用的不同条件设置不同的背景颜色。我可以根据不同的条件设置背景颜色,但不能制作TextView圆形。那么如何做到这一点。请帮我解决这个问题。

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"));              
        }

回答by yoah

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:

如果您的颜色数量相对较少,您可以为每种颜色创建一个可绘制文件,例如创建一个文件 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:

<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);

回答by Sudhasri

To add up to the accepted answer, adding size tag inside shape and making sure height and width is large enough will make sure the background is circle even if textView has many characters!

为了增加接受的答案,在形状内添加尺寸标签并确保高度和宽度足够大将确保背景是圆形,即使 textView 有很多字符!

<shape>
 <solid android:color="#f00" />
 <corners
    android:topLeftRadius="30dp"
   android:topRightRadius="30dp"
     android:bottomLeftRadius="30dp"
     android:bottomRightRadius="30dp"
     />
 <size 
  android:height="25dp"
   android:width="25dp"/>
</shape>
<shape>
 <solid android:color="#f00" />
 <corners
    android:topLeftRadius="30dp"
   android:topRightRadius="30dp"
     android:bottomLeftRadius="30dp"
     android:bottomRightRadius="30dp"
     />
 <size 
  android:height="25dp"
   android:width="25dp"/>
</shape>

回答by iftach barshem

TextView textView = (TextView) findViewById(R.id.my_text_view);             
Drawable drawable = textView.getBackground();
drawable.setColorFilter(getResources().getColor(color), PorterDuff.Mode.SRC_IN);

works for me

为我工作

回答by Anton Donov

You can also achieve this by setting color to background drawable like this

您也可以通过将颜色设置为可绘制的背景来实现此目的

TextView textView = (TextView) findViewById(R.id.my_text_view);
((GradientDrawable)textView.getBackground()).setColor(R.color.my_color);

回答by habib ullah

Add circular_hired.xml file into your drawable,

将circular_hired.xml 文件添加到您的drawable 中,

  <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >


            <solid android:color="@color/map_hide_bg"/>
            <stroke android:width="1dip" android:color="@color/map_hide_bg" />

    </shape>

but in you layout design xml file , you have to fix the width and height of textview. it will give the circular form.

但是在你的布局设计xml文件中,你必须固定textview的宽度和高度。它将给出圆形形式。

<TextView
            android:id="@+id/tvMessages"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:background="@drawable/circular_message"
            android:padding="4dp"
            android:text="5"
            android:textColor="@color/white_color"
            android:textSize="@dimen/txt12"
            android:visibility="visible"
             />

i sure it will help you,

我相信它会帮助你,