亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android TextView字體顏色設(shè)置方法小結(jié)

 更新時(shí)間:2016年02月20日 10:33:53   作者:fengyee_zju  
這篇文章主要介紹了Android TextView字體顏色設(shè)置方法,結(jié)合實(shí)例形式總結(jié)分析了Android開(kāi)發(fā)中TextView設(shè)置字體顏色的常用技巧,需要的朋友可以參考下

本文實(shí)例總結(jié)了Android TextView字體顏色設(shè)置方法。分享給大家供大家參考,具體如下:

對(duì)于setTextView(int a)這里的a是傳進(jìn)去顏色的值。例如,紅色0xff0000是指0xff0000如何直接傳入R.color.red是沒(méi)有辦法設(shè)置顏色的,只有通過(guò)文章中的第三種方法先拿到資源的顏色值再傳進(jìn)去。

tv.setTextColor(this.getResources().getColor(R.color.red));

關(guān)鍵字: android textview color

TextView的字體設(shè)置方法:

1、直接通過(guò)配置文件設(shè)置
2、在Activity類中進(jìn)行設(shè)置

第一種方式很簡(jiǎn)單,用于靜態(tài)或初始文字顏色的設(shè)置,方法如下:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  >
<TextView
  android:id="@+id/tv01"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  android:autoLink="all"
  android:textColor="@color/red"
  />
</LinearLayout>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <drawable name="white">#FFFFFF</drawable>
  <drawable name="dark">#000000</drawable>
  <drawable name="red">#FF0000</drawable>
</resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">地址:http://yahaitt.javaeye.com</string>
  <string name="app_name">丫梨的筆記本</string>
</resources>

上面將資源部分分成了3個(gè)部分,目的是為了清晰,當(dāng)然你也可以只建一個(gè)xml文件放在res目錄下,而且文件名稱可以隨便命名。

注意兩個(gè)地方:

1、main.xml的TextView標(biāo)簽中:android:textColor="@color/red"

2、color.xml中:<color name="red">#FF0000</color>

@color指獲取資源文件中(所有res目錄下的xml文件)的<color>標(biāo)簽

/red指在標(biāo)簽下找其name值為red的內(nèi)容,此時(shí)其值為#FF0000

因此,這里我們還可以這樣做:android:textColor="@drawable/red"

@drawable指獲取資源文件中<drawable>標(biāo)簽

/red指在標(biāo)簽下找其name值為red的內(nèi)容

以此類推,相信你也就知道了如果是在strings.xml中該怎么做了。

下面看看第二種方式:在Activity類中進(jìn)行設(shè)置

1、先將main.xml改成如下,即去掉android:textColor="@color/red":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  >
<TextView
  android:id="@+id/tv01"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  android:autoLink="all"
  />
</LinearLayout>

2、修改Activity的onCreate方法,這里我的Activity是Study03_01,原始代碼如下:

package yahaitt.study03_01;
import android.app.Activity;
import android.os.Bundle;
public class Study03_01 extends Activity {    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}

第一步:獲得文本控件TextView,取名為tv

第二步:通過(guò)TextView的setTextColor方法進(jìn)行文本顏色的設(shè)置,這里可以有3種方式進(jìn)行設(shè)置:

第1種:tv.setTextColor(android.graphics.Color.RED);//系統(tǒng)自帶的顏色類

第2種:tv.setTextColor(0xffff00ff);//0xffff00ff是int類型的數(shù)據(jù),分組一下0x|ff|ff00ff,0x是代表顏色整數(shù)的標(biāo)記,ff是表示透明度,ff00ff表示顏色,注意:這里ffff00ff必須是8個(gè)的顏色表示,不接受ff00ff這種6個(gè)的顏色表示。

第3種:tv.setTextColor(this.getResources().getColor(R.color.red));//通過(guò)獲得資源文件進(jìn)行設(shè)置。根據(jù)不同的情況R.color.red也可以是R.string.red或者R.drawable.red,當(dāng)然前提是需要在相應(yīng)的配置文件里做相應(yīng)的配置,如:

<color name="red">#FF0000</color>
<drawable name="red">#FF0000</drawable>
<string name="red">#FF0000</string>

詳細(xì)的代碼如下:

package yahaitt.study03_01;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class Study03_01 extends Activity {
  private TextView tv;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView)this.findViewById(R.id.tv01);
    //tv.setTextColor(Color.RED);
    //tv.setTextColor(0xff000000);
  }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android通信方式總結(jié)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

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

相關(guān)文章

  • Android studio 引用aar 進(jìn)行java開(kāi)發(fā)的操作步驟

    Android studio 引用aar 進(jìn)行java開(kāi)發(fā)的操作步驟

    這篇文章主要介紹了Android studio 引用aar 進(jìn)行java開(kāi)發(fā)的操作步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Android實(shí)現(xiàn)文件解壓帶進(jìn)度條功能

    Android實(shí)現(xiàn)文件解壓帶進(jìn)度條功能

    本文通過(guò)實(shí)例代碼給大家介紹了android實(shí)現(xiàn)文件解壓帶進(jìn)度條效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-08-08
  • 如何利用Android仿微博正文鏈接交互效果

    如何利用Android仿微博正文鏈接交互效果

    最近在開(kāi)發(fā)中遇到了各種坑,所以分享一下,希望能給大家貢獻(xiàn)點(diǎn)經(jīng)驗(yàn),下面這篇文章主要給大家介紹了關(guān)于如何利用Android仿微博正文鏈接交互效果的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Android實(shí)現(xiàn)串口通信

    Android實(shí)現(xiàn)串口通信

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)串口通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • android ocr——身份證識(shí)別的功能實(shí)現(xiàn)

    android ocr——身份證識(shí)別的功能實(shí)現(xiàn)

    本篇文章主要介紹了android ocr——身份證識(shí)別的功能實(shí)現(xiàn),具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-11-11
  • Android 中 GridView嵌套在ScrollView里只有一行的解決方法

    Android 中 GridView嵌套在ScrollView里只有一行的解決方法

    本文給大家?guī)?lái)兩種有關(guān)Android 中 GridView嵌套在ScrollView里只有一行的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-10-10
  • Android使用ViewPager實(shí)現(xiàn)導(dǎo)航

    Android使用ViewPager實(shí)現(xiàn)導(dǎo)航

    本文主要介紹了Android使用ViewPager實(shí)現(xiàn)導(dǎo)航的方法代碼。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • Android 單線程模型詳解及實(shí)例

    Android 單線程模型詳解及實(shí)例

    這篇文章主要介紹了Android 單線程模型詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Android webview 內(nèi)存泄露的解決方法

    Android webview 內(nèi)存泄露的解決方法

    這篇文章主要介紹了Android webview 內(nèi)存泄露的解決方法的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 你該知道的Gradle配置知識(shí)總結(jié)

    你該知道的Gradle配置知識(shí)總結(jié)

    這篇文章主要給大家介紹了關(guān)于Gradle配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考學(xué)習(xí),下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10

最新評(píng)論