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

android 設(shè)置控件的顏色字體的方法

 更新時間:2013年09月04日 15:59:15   作者:  
這篇文章介紹了android 設(shè)置控件的顏色字體的方法,有需要的朋友可以參考一下

1.用代碼設(shè)置控件的顏色:

復(fù)制代碼 代碼如下:

    int b =  getResources().getColor(R.drawable.blue);//得到配置文件里的顏色
    mButton.setTextColor(b);
   
2.設(shè)置空間的字體:
方式一:mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//設(shè)置字體
   注意:1.保證文件一定是ttf格式;2.放到assets/fonts目錄下;3.如果找不到相應(yīng)的字體不會報錯,只是在運行的時候顯示不出來
方式二: fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用內(nèi)部支持的方式設(shè)置

復(fù)制代碼 代碼如下:

package com.oyzz.ch3_6;

import android.app.Activity;
/*必須引用graphics.Color才能使用Color.*的對象*/
import android.graphics.Color;
import android.graphics.Typeface;

import android.os.Bundle;
import android.view.View;

/*必須引用 widget.Button才能聲明使用Button對象*/
import android.widget.Button;

/*必須引用 widget.TextView才能聲明使用TestView對象*/
import android.widget.TextView;
public class Ch3_6 extends Activity
{
  private Button mButton;
  private TextView mText;
  private int[] mColors;
  private int colornum;
  private Button fontButton;

  /** Called when the activity is first created. */
  @Override

  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /*通過findViewById構(gòu)造器來使用main.xml與string.xml
    中button與textView的參數(shù)*/
    mButton=(Button) findViewById(R.id.mybutton);
    mText= (TextView) findViewById(R.id.mytext);
    fontButton=(Button) findViewById(R.id.mybutton1);

    /*聲明并構(gòu)造一整數(shù)array來存儲欲使用的文字顏色*/
    mColors = new int[]
                      {
    Color.BLACK, Color.RED, Color.BLUE,
    Color.GREEN, Color.MAGENTA, Color.YELLOW
                       };
    colornum=0;
    //得到color.xml文件里的顏色
    int b =  getResources().getColor(R.drawable.blue);//得到配置文件里的顏色
    mButton.setTextColor(b);
    /*使用setOnClickListener讓按鈕聆聽事件*/
    mButton.setOnClickListener(new View.OnClickListener()
    {            
      /*使用onClick讓用戶點下按鈕來驅(qū)動變動文字顏色*/
      public void onClick(View v)
      {       
        if (colornum < mColors.length)
        {
          mText.setTextColor(mColors[colornum]);
          colornum++;
        }
        else
          colornum=0;
       } 
    });

    fontButton.setOnClickListener(new Button.OnClickListener() {
  public void onClick(View v) {
   mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//設(shè)置字體
   fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用內(nèi)部支持的方式設(shè)置
  }
 });
  }
}


main.xml:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>

    <!-- Layout使用白色的背景 -->
<LinearLayout
  android:id="@+id/widget27"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  >
  <!--
      文字使用mytext作為id使用string.xml中
      的textview_str參數(shù) 預(yù)設(shè)文字顏色為按灰色
  -->
  <TextView
  android:id="@+id/mytext"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/textview_str"
  android:textColor="@drawable/darkgray"
  >
  </TextView>
  <!-- 按鈕以mybutton作為id使用string.xml中
   的button_str參數(shù)
  -->
  <Button
  android:id="@+id/mybutton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/button_str"
  >
  </Button>
   <Button
  android:id="@+id/mybutton1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="字體"
  >
  </Button>
</LinearLayout>

color.xml:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
  <resources>
  <drawable name="darkgray">#404040ff</drawable>
  <drawable name="black">#000</drawable>
  <drawable name="red">#ff00ff</drawable>
  <drawable name="green">#0ff0ff</drawable>
  <drawable name="lightgray">#c0c0c0ff</drawable>
  <drawable name="white">#ffffffff</drawable>
  <drawable name="yellow">#ffFF33ff</drawable>
  <drawable name="blue">#00ffff</drawable>
  <drawable name="gray">#808080ff</drawable>
  <drawable name="magenta">#ff6699ff</drawable>
  <drawable name="cyan">#66ffffff</drawable>
</resources>

strings.xml:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, Ex03_13</string>
  <string name="app_name">Ex03_13</string>
  <string name="textview_str">轉(zhuǎn)吧七彩霓虹燈</string>
  <string name="button_str">按我</string>
</resources>

相關(guān)文章

  • Android中Textview超鏈接實現(xiàn)方式

    Android中Textview超鏈接實現(xiàn)方式

    TextView中的超鏈接可以通過幾種方式實現(xiàn):1.Html.fromHtml,2.Spannable,3.Linkify.addLinks。下面分別進(jìn)行測試,包括修改字體樣式,下劃線樣式,點擊事件等,需要的朋友可以參考下
    2016-02-02
  • Android 九宮格的實現(xiàn)方法

    Android 九宮格的實現(xiàn)方法

    今天在瀏覽網(wǎng)頁的時候看到了一篇有關(guān)九宮格實現(xiàn)的博文,感覺挺有意思。所以自己模仿做了一個,先貼出代碼如下:
    2013-05-05
  • android ImageView 的幾點經(jīng)驗總結(jié)

    android ImageView 的幾點經(jīng)驗總結(jié)

    本篇文章是對android中ImageView的使用技巧進(jìn)行了幾點經(jīng)驗總結(jié),需要的朋友參考下
    2013-06-06
  • Android studio配置lambda表達(dá)式教程

    Android studio配置lambda表達(dá)式教程

    Java 8的一個大亮點是引入Lambda表達(dá)式,使用它設(shè)計的代碼會更加簡潔。接下來通過本文給大家介紹Android studio配置lambda表達(dá)式教程,需要的朋友參考下吧
    2017-05-05
  • Android 中 Fragment的使用大全

    Android 中 Fragment的使用大全

    通常地 fragment做為宿主activity UI的一部分, 被作為activity整個view hierarchy的一部分被嵌入。本篇文章給大家介紹android fragment使用,需要的朋友一起看看吧
    2015-10-10
  • Android 中三種啟用線程的方法總結(jié)

    Android 中三種啟用線程的方法總結(jié)

    下面小編就為大家?guī)硪黄狝ndroid 中三種啟用線程的方法總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Android通過startService播放背景音樂

    Android通過startService播放背景音樂

    這篇文章主要介紹了Android通過startService播放背景音樂簡單示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Kotlin泛型的型變之路演變示例詳解

    Kotlin泛型的型變之路演變示例詳解

    這篇文章主要為大家介紹了Kotlin泛型的型變之路演變示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android子線程與更新UI問題的深入講解

    Android子線程與更新UI問題的深入講解

    首先和其他許多的GUI庫一樣,Android的UI線程是不安全的。所以下面這篇文章主要給大家介紹了關(guān)于Android子線程與更新UI問題的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Android把Bitmap保存為PNG圖像文件的簡單代碼

    Android把Bitmap保存為PNG圖像文件的簡單代碼

    這篇文章主要介紹了Android把Bitmap保存為PNG圖像文件的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-08-08

最新評論