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 中以粗体和多色显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在我的 TextView 中以粗体和多色显示文本


基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01