歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android --- TextView中文本的不同顏色

Android --- TextView中文本的不同顏色

日期:2017/3/1 10:23:25   编辑:Linux編程

面試其中有一道題是:

TextView中文本的顏色設置?例如:中軟國際,其中中軟為紅色,國際為綠色。

方法一:

  1. <LinearLayout
  2. Android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal">
  5. <TextView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:text="中軟"
  9. android:textColor="#FFFF0000" />
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="國際"
  14. android:textColor="#FF00FF00" />
  15. </LinearLayout>

方法二:

  1. package me.lechao;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.text.Html;
  5. import android.widget.TextView;
  6. public class MainActivity extends Activity {
  7. /** 利用HTML語言,改變文字顏色 */
  8. private void init() {
  9. String str = "<font color='red'>中軟</font>"
  10. + "<font color= 'green'>國際</font>";
  11. TextView tv = new TextView(this);
  12. tv.setText(Html.fromHtml(str));
  13. }
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. init();
  19. }
  20. }

PS:凡是不要想的太復雜。簡簡單單就好。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved