How do I show text in my TextView with bold and multi coloured(如何在我的 TextView 中以粗体和多色显示文本)
问题描述
我的要求是在 TextView 中以彩虹色文本和粗体等多色显示文本,我该如何实现这一点.我需要使用 java 代码动态显示它们..
my requirement is to show the Text in the TextView in multicoloured like rainbow color text and Bold, how can I achieve this.and I need to display them dynamically using java code..
TextView text=new TextView(context);
text.setText(status);
text.setBackgroundResource(R.drawable.grd_btn);
text.setGravity(Gravity.CENTER);
text.setPadding(2, 0, 2, 0);
text.setTypeface(font2,Typeface.BOLD);
text.setTextColor(Color.WHITE);
推荐答案
对不起,我的朋友耽搁了.必须解决您的问题,这需要一段时间.所以首先是输出,
Hey sorry for the delay my friend. Had to work on your problem and it took a while. So first goes the output,
所以假设上面显示的是您需要的输出,这里是它的代码.
So assuming that the above shown is your needed output here is the code to it.
xml 文件
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"/>
res 文件(比如 strings.xml)
<color name="violet">#9400D3</color>
<color name="indigo">#4B0082</color>
<color name="blue">#0000FF</color>
<color name="green">#00FF00</color>
<color name="yellow">#FFFF00</color>
<color name="orange">#FF7F00</color>
<color name="red">#FF0000</color>
您的 java 文件
TextView textView = (TextView)findViewById(R.id.textView1);
Shader textShader=new LinearGradient(0, 0, 0, 20,
new int[]{getResources().getColor(R.color.violet),getResources().getColor(R.color.indigo),
getResources().getColor(R.color.blue),
getResources().getColor(R.color.green),
getResources().getColor(R.color.yellow),
getResources().getColor(R.color.orange),
getResources().getColor(R.color.red)},
new float[]{0,0.2f,0.4f,0.6f,0.8f,0.9f,1}, TileMode.CLAMP);
textView.getPaint().setShader(textShader);
textView.setTextSize(20);
就是这样.对于您的大胆风格,请点击我之前回答的以下链接,
That's it. And for your bold style follow the below link of my previous answer,
https://stackoverflow.com/a/5169604/603744
这篇关于如何在我的 TextView 中以粗体和多色显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!