android.graphics.Color 包含顔色值
Color.BLACK 黑色
Color.BLUE 藍色
Color.CYAN 青綠色
Color.DKGRAY 灰黑色
Color.GRAY 灰色
Color.GREEN 綠色
Color.LTGRAY 淺灰色
Color.MAGENTA 紅紫色
Color.RED 紅色
Color.TRANSPARENT 透明
Color.WHITE 白色
Color.YELLOW 黃色
程式設計實作顔色變幻
① 建立工程
② 修改mainActivity.java 檔案,添加12 個TextView 對象變量,一個LinearLayout 對象變量、一個WC
整數變量、一個LinearLayout.LayoutParams 變量。
package com.example.forwarding;
import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Forwarding extends ActionBarActivity {
/** Called when the activity is first created. */
/* 定義使用的對象 */
private LinearLayout myLayout;
private LinearLayout.LayoutParams layoutP;
private int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private TextView black_TV, blue_TV, cyan_TV, dkgray_TV, gray_TV, green_TV,
ltgray_TV, magenta_TV, red_TV, transparent_TV, white_TV, yellow_TV;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 執行個體化一個LinearLayout布局對象 */
myLayout = new LinearLayout(this);
/* 設定LinearLayout的布局為垂直布局 */
myLayout.setOrientation(LinearLayout.VERTICAL);
/* 設定LinearLayout布局背景圖檔 */
//myLayout.setBackgroundColor(Color.BLACK);
// myLayout.setBackgroundResource(R.drawable.back);
/* 加載主屏布局 */
setContentView(myLayout);
/* 執行個體化一個LinearLayout布局參數,用來添加View */
layoutP = new LinearLayout.LayoutParams(WC, WC);
/* 構造執行個體化TextView對象 */
constructTextView();
/* 把TextView添加到LinearLayout布局中 */
addTextView();
/* 設定TextView文本顔色 */
setTextViewColor();
/* 設定TextView文本内容 */
setTextViewText();
}
/* 設定TextView文本内容 */
public void setTextViewText() {
black_TV.setText("黑色");
blue_TV.setText("藍色");
cyan_TV.setText("青綠色");
dkgray_TV.setText("灰黑色");
gray_TV.setText("灰色");
green_TV.setText("綠色");
ltgray_TV.setText("淺灰色");
magenta_TV.setText("紅紫色");
red_TV.setText("紅色");
transparent_TV.setText("透明");
white_TV.setText("白色");
yellow_TV.setText("黃色");
}
/* 設定TextView文本顔色 */
public void setTextViewColor() {
black_TV.setTextColor(Color.BLACK);
blue_TV.setTextColor(Color.BLUE);
dkgray_TV.setTextColor(Color.DKGRAY);
gray_TV.setTextColor(Color.GRAY);
green_TV.setTextColor(Color.GREEN);
ltgray_TV.setTextColor(Color.LTGRAY);
magenta_TV.setTextColor(Color.MAGENTA);
red_TV.setTextColor(Color.RED);
transparent_TV.setTextColor(Color.TRANSPARENT);
white_TV.setTextColor(Color.WHITE);
yellow_TV.setTextColor(Color.YELLOW);
}
/* 構造執行個體化TextView對象 */
public void constructTextView() {
black_TV = new TextView(this);
blue_TV = new TextView(this);
cyan_TV = new TextView(this);
dkgray_TV = new TextView(this);
gray_TV = new TextView(this);
green_TV = new TextView(this);
ltgray_TV = new TextView(this);
magenta_TV = new TextView(this);
red_TV = new TextView(this);
transparent_TV = new TextView(this);
white_TV = new TextView(this);
yellow_TV = new TextView(this);
}
/* 把TextView添加到LinearLayout布局中 */
public void addTextView() {
myLayout.addView(black_TV, layoutP);
myLayout.addView(blue_TV, layoutP);
myLayout.addView(cyan_TV, layoutP);
myLayout.addView(dkgray_TV, layoutP);
myLayout.addView(gray_TV, layoutP);
myLayout.addView(green_TV, layoutP);
myLayout.addView(ltgray_TV, layoutP);
myLayout.addView(magenta_TV, layoutP);
myLayout.addView(red_TV, layoutP);
myLayout.addView(transparent_TV, layoutP);
myLayout.addView(white_TV, layoutP);
myLayout.addView(yellow_TV, layoutP);
}
}
效果如下:
