Android初级教程八——TextView编程实现颜色变换

来源:互联网 发布:c语言中文网vip资料1t 编辑:程序博客网 时间:2024/06/05 10:02

View 和Viewgroup

更改与显示文字标签


TextView 标签的使用

① 导入TextView 包

import android.widget.TextView;

② 在mainActivity.java 中声明一个TextView

private TextView mTextView01;

③ 在main.xml 中定义一个TextView

<TextView android:text="TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="61px"
android:layout_y="69px">
</TextView>

④ 利用findViewById()方法获取main.xml 中的TextView

mTextView01 = (TextView) findViewById(R.id.TextView01);

⑤ 设置TextView 标签内容

String str_2 = "欢迎来到Android 的TextView 世界...";
mTextView01.setText(str_2);

⑥ 设置文本超级链接

<TextView

android:id="@+id/TextView02"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:autoLink="all"

android:text="请访问Android 开发者:http://developer.android.com/index.html" >

</TextView>



编程实现颜色变幻

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 zyf.ManyColorME;
/*导入要使用的包*/
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ManyColorME extends Activity {
/** 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.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);
}
}



原创粉丝点击