android 設置控件的顏色,字體

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

 

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構造器來使用main.xml與string.xml
    中button與textView的參數*/
    mButton=(Button) findViewById(R.id.mybutton);
    mText= (TextView) findViewById(R.id.mytext);
    fontButton=(Button) findViewById(R.id.mybutton1);

    /*聲明並構造一整數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讓用戶點下按鈕來驅動變更文字顏色*/
      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"));//設置字體
   fontButton.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));//用內部支持的方式設置
  }
 });
  }
}

main.xml:

<?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參數 預設文字顏色為按灰色
  -->
  <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參數
  -->
  <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:

<?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:

<?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">轉吧七彩霓虹燈</string>
  <string name="button_str">按我</string>
</resources>
代碼
相關文章
相關標籤/搜索