問題描述
我的要求是在 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 中以粗體和多色顯示文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!