欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程設(shè)置TextView顏色setTextColor用法實例

 更新時間:2016年01月13日 12:20:45   作者:chenguang79  
這篇文章主要介紹了Android編程設(shè)置TextView顏色setTextColor用法,結(jié)合實例形式分析了Android設(shè)置TextView顏色setTextColor、ColorStateList等方法的使用技巧與布局文件的設(shè)置方法,需要的朋友可以參考下

本文實例講述了Android編程設(shè)置TextView顏色setTextColor用法。分享給大家供大家參考,具體如下:

android中設(shè)置TextView的顏色有方法setTextColor,這個方法被重載了,可以傳入兩種參數(shù)。

public void setTextColor(int color) {
 mTextColor = ColorStateList.valueOf(color);
 updateTextColors();
}
public void setTextColor(ColorStateList colors) {
 if (colors == null) {
  throw new NullPointerException();
 }
 mTextColor = colors;
 updateTextColors();
}

下邊就分別寫出怎么使用這兩個方法設(shè)置TextView的顏色:

TextView tv = new TextView(this);
tv.setText("Test set TextView's color.");
//方案一:代碼中通過argb值的方式
tv.setTextColor(Color.rgb(255, 255, 255));

這種方法也就是傳入int color值,這個int不是R文件中自動分配的int值,所以要注意。這是Color類中的靜態(tài)方法構(gòu)造出來的顏色int值。

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

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

還有種方法:

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

全部代碼:

package com.txlong;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class ListViewDemoActivity extends Activity {
 // private ListView listView;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText("Test set TextView's color.");
  //方案一:通過ARGB值的方式
  /**
   * set the TextView color as the 0~255's ARGB,These component values
   * should be [0..255], but there is no range check performed, so if they
   * are out of range, the returned color is undefined
   */
//  tv.setTextColor(Color.rgb(255, 255, 255));
  /**
   * set the TextView color as the #RRGGBB #AARRGGBB 'red', 'blue',
   * 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow',
   * 'lightgray', 'darkgray'
   */
  tv.setTextColor(Color.parseColor("#FFFFFF"));
  /** 原來不知道有上邊的方法,就用這個笨笨方法了 */
//  String StrColor = null;
//  StrColor = "FFFFFFFF";
//  int length = StrColor.length();
//  if (length == 6) {
//   tv.setTextColor(Color.rgb(
//     Integer.valueOf(StrColor.substring(0, 2), 16),
//     Integer.valueOf(StrColor.substring(2, 4), 16),
//     Integer.valueOf(StrColor.substring(4, 6), 16)));
//  } else if (length == 8) {
//   tv.setTextColor(Color.argb(
//     Integer.valueOf(StrColor.substring(0, 2), 16),
//     Integer.valueOf(StrColor.substring(2, 4), 16),
//     Integer.valueOf(StrColor.substring(4, 6), 16),
//     Integer.valueOf(StrColor.substring(6, 8), 16)));
//  }
  //方案二:通過資源引用
//  tv.setTextColor(getResources().getColor(R.color.my_color));
  //方案三:通過資源文件寫在String.xml中
//  Resources resource = (Resources) getBaseContext().getResources();
//  ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
//  if (csl != null) {
//   tv.setTextColor(csl);
//  }
  //方案四:通過xml文件,如/res/text_color.xml
//  XmlPullParser xrp = getResources().getXml(R.color.text_color);
//  try {
//   ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
//   tv.setTextColor(csl);
//  } catch (Exception e) {
//  }
  // listView = new ListView(this);
  //
  // Cursor cursor = getContentResolver().query(
  // Uri.parse("content://contacts/people"), null, null, null, null);
  //
  // startManagingCursor(cursor);
  //
  // ListAdapter listAdapter = new SimpleCursorAdapter(this,
  // android.R.layout.simple_expandable_list_item_2, cursor,
  // new String[] { "name", "name" }, new int[] {
  // android.R.id.text1, android.R.id.text2 });
  //
  // listView.setAdapter(listAdapter);
  // setContentView(listView);
  setContentView(tv);
 }
}

String.xml文件為:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">Hello World, ListViewDemoActivity!</string>
 <string name="app_name">ListViewDemo</string>
 <color name="my_color">#FFFFFF</color>
</resources>

/res/color/text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" android:color="#FF111111"/>
  <!-- pressed -->
  <item android:state_focused="true" android:color="#FF222222"/>
  <!-- focused -->
  <item android:state_selected="true" android:color="#FF333333"/>
  <!-- selected -->
  <item android:state_active="true" android:color="#FF444444"/>
  <!-- active -->
  <item android:state_checkable="true" android:color="#FF555555"/>
  <!-- checkable -->
  <item android:state_checked="true" android:color="#FF666666"/>
  <!-- checked -->
  <item android:state_enabled="true" android:color="#FF777777"/>
  <!-- enabled -->
  <item android:state_window_focused="true" android:color="#FF888888"/>
  <!-- window_focused -->
</selector>

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容

    Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容

    這篇文章主要為大家詳細(xì)介紹了Android設(shè)備藍(lán)牙連接掃描槍獲取掃描內(nèi)容,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • flutter 實現(xiàn)點擊下拉欄微信右上角彈出窗功能

    flutter 實現(xiàn)點擊下拉欄微信右上角彈出窗功能

    這篇文章主要介紹了flutter 實現(xiàn)彈出窗點擊下拉欄微信右上角彈出窗功能,這段代碼使用的是PopupRoute這個路由類進(jìn)行實現(xiàn) 的,分步驟通過實例代碼講解的非常詳細(xì),需要的朋友可以參考下
    2021-05-05
  • Android listview點贊問題分析

    Android listview點贊問題分析

    這篇文章主要為大家詳細(xì)解析了Android listview點贊功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android實現(xiàn)百度地圖兩點畫弧線

    Android實現(xiàn)百度地圖兩點畫弧線

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)百度地圖兩點畫弧線,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Android移動開發(fā)recycleView的頁面點擊跳轉(zhuǎn)設(shè)計實現(xiàn)

    Android移動開發(fā)recycleView的頁面點擊跳轉(zhuǎn)設(shè)計實現(xiàn)

    這篇文章主要介紹了Android移動開發(fā)recycleView的頁面點擊跳轉(zhuǎn)設(shè)計實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Android?App隱私合規(guī)檢測輔助工具Camille詳解

    Android?App隱私合規(guī)檢測輔助工具Camille詳解

    Camille是一款A(yù)ndroid?App隱私合規(guī)檢測輔助工具,現(xiàn)如今APP隱私合規(guī)十分重要,各監(jiān)管部門不斷開展APP專項治理工作及核查通報,不合規(guī)的APP通知整改或直接下架,下面小編給大家介紹下Android?App隱私合規(guī)檢測輔助工具Camille,感興趣的朋友一起看看吧
    2022-02-02
  • UiOS開發(fā)中ITextView回收或關(guān)閉鍵盤使用方法總結(jié)

    UiOS開發(fā)中ITextView回收或關(guān)閉鍵盤使用方法總結(jié)

    iOS開發(fā)中,發(fā)現(xiàn)UITextView沒有像UITextField中textFieldShouldReturn:這樣的方法,那么要實現(xiàn)UITextView關(guān)閉鍵盤,必須使用其他的方法,下面是可以使用的幾種方法,需要的朋友參考下吧
    2016-11-11
  • Android開發(fā)實現(xiàn)消除屏幕鎖的方法

    Android開發(fā)實現(xiàn)消除屏幕鎖的方法

    這篇文章主要介紹了Android開發(fā)實現(xiàn)消除屏幕鎖的方法,結(jié)合實例形式較為詳細(xì)的分析了Android鎖屏的原理及消除屏幕鎖的相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • Kotlin注解與反射的定義及創(chuàng)建使用詳解

    Kotlin注解與反射的定義及創(chuàng)建使用詳解

    這篇文章主要為大家介紹了Kotlin注解與反射的定義及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Flutter?文字中劃線動畫StrikeThroughTextAnimation

    Flutter?文字中劃線動畫StrikeThroughTextAnimation

    這篇文章主要為大家介紹了Flutter?文字中劃線動畫StrikeThroughTextAnimation示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04

最新評論