Android中設定TextView的顏色setTextColor,textviewsetcolor

來源:互聯網
上載者:User

Android中設定TextView的顏色setTextColor,textviewsetcolor

tv.setTextColor(Color.parseColor("#FFFFFF"));
tv.setTextColor(Color.WHITE);

  tv.setTextColor(Color.rgb(255, 255, 255));
  //注意Color是大寫C,不是color.holo_orange_dark,這樣錯誤並沒效果的

  tv.setBackgroundResource(R.drawable.icon_bg_rectang_stroke);

這種方法也就是傳入int color值,這個int不是R檔案中自動分配的int值,所以要注意。這是Color類中的靜態方法構造出來的顏色int值。

 -------------------------------------------------------------------------------------

另一種方法:tv.setTextColor(this.getResources().getColor(R.color.red));

//通過獲得資源檔進行設定。根據不同的情況R.color.red也可以是R.string.red或者R.drawable.red,

//當然前提是需要在相應的設定檔裡做相應的配置,如:
#FF0000
< drawable name="red">#FF0000

< string name="red">#FF0000

注意上面的第三種方法,千萬不能把自己定義的顏色直接當成參數,例如:

tv.setTextColor(R.color.red);這種情況會出現顏色錯誤!

--------------------------------------------------------------------

另一種方法:

1 Resources resource = (Resources) getBaseContext().getResources();2 ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);3 if (csl != null) {4     tv.setTextColor(csl);5 }

這種方法是通過ColorStateList得到xml中的配置的顏色的。好多需要xml中配置的都要類似這樣的映射xml檔案。

還有種方法:

1 XmlResourceParser xrp = getResources().getXml(R.color.my_color);2 try {3     ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);4     tv.setTextColor(csl);5 } catch (Exception e) {6 }

android中設定TextView的顏色有方法setTextColor,這個方法被重載了,可以傳入兩種參數。

 1 public void setTextColor(int color) { 2     mTextColor = ColorStateList.valueOf(color); 3     updateTextColors(); 4 } 5  6 public void setTextColor(ColorStateList colors) { 7     if (colors == null) { 8         throw new NullPointerException(); 9     }10 11     mTextColor = colors;12     updateTextColors();13 }

 

查看顏色值:(

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.