如何显示在我的TextView中以大胆多彩色文本 [英] How do I show text in my TextView with bold and multi coloured

查看:191
本文介绍了如何显示在我的TextView中以大胆多彩色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是显示在五彩如虹彩色文本和大胆的TextView的文本,我怎么能实现this.and我需要动态地用java code,以显示他们..

  TextView的文本=新的TextView(上下文);
                        text.setText(状态);
                    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);
 

解决方案

嘿抱歉耽搁我的朋友。不得不工作在你的问题,过了好一会儿。所以,首先进入的输出,

所以假设上面显示的是您需要的输出这里是code到它。

XML文件

 <的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真
    机器人:文本=@字符串/参考hello world/>
 

资源文件(说的strings.xml)

 <颜色名称=紫罗兰>#9400D3< /彩色>
<颜色名称=靛蓝>#4B0082< /彩色>
<颜色名称=蓝色>#0000FF< /彩色>
<颜色名称=绿色>#00FF00< /彩色>
<颜色名称=黄色>#FFFF00< /彩色>
<颜色名称=橙色>#FF7F00< /彩色>
<颜色名称=红>#FF0000< /彩色>
 

你的java文件

 的TextView TextView的=(的TextView)findViewById(R.id.textView1);
        着色textShader =新的LinearGradient(0,0,0,20,
                新的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)},
                新浮法[] {0,0.2f,0.4f,0.6f,0.8f,0.9F,1},TileMode.CLAMP);
        textView.getPaint()setShader(textShader)。
        textView.setTextSize(20);
 

就是这样。并为您的大胆风格跟随我的previous回答下面的链接,

http://stackoverflow.com/a/5169604/603744

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 file

<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 file (say 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>

your java file

     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,

http://stackoverflow.com/a/5169604/603744

这篇关于如何显示在我的TextView中以大胆多彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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