gpt4 book ai didi

android - 如何使TextView形状为圆形并根据条件设置不同的背景颜色

转载 作者:IT老高 更新时间:2023-10-28 22:00:46 33 4
gpt4 key购买 nike

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

我使用过的代码:

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

最佳答案

如果你的颜色数量比较少,你可以为每种颜色创建一个drawable文件,例如创建一个文件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>

然后分配定义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"
/>

注意宽度是背景角半径的两倍。

从代码更改颜色:

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

关于android - 如何使TextView形状为圆形并根据条件设置不同的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10316354/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com