text="TextView01"
android:
id="@+id/TextView01"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_x="61px"
android:
layout_y="69px">
4 利用findViewById()方法获取main.xml中的TextView
mTextView01=(TextView)findViewById(R.id.TextView01);
5 设置TextView标签内容
Stringstr_2="欢迎来到Android的TextView世界...";
mTextView01.setText(str_2);
6 设置文本超级链接
android:
id="@+id/TextView02"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
autoLink="all"
android:
text="请访问Android开发者:
android.graphics.Color实践----Color颜色变幻
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
黑色
蓝色
青绿色
灰黑色
灰色
绿色
浅灰色
红紫色
红色
透明
白色
黄色
编程实现颜色变幻
1 新建工程
2 修改mainActivity.java文件,添加12个TextView对象变量,一个LinearLayout对象变量、一个WC整数变量、一个LinearLayout.LayoutParams变量。
packagezyf.ManyColorME;
/*导入要使用的包*/
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;
publicclassManyColorMEextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
/*定义使用的对象*/
privateLinearLayoutmyLayout;
privateLinearLayout.LayoutParamslayoutP;
privateintWC=LinearLayout.LayoutParams.WRAP_CONTENT;
privateTextViewblack_TV,blue_TV,cyan_TV,dkgray_TV,
gray_TV,green_TV,ltgray_TV,magenta_TV,red_TV,
transparent_TV,white_TV,yellow_TV;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
/*实例化一个LinearLayout布局对象*/
myLayout=newLinearLayout(this);
/*设置LinearLayout的布局为垂直布局*/
myLayout.setOrientation(LinearLayout.VERTICAL);
/*设置LinearLayout布局背景图片*/
myLayout.setBackgroundResource(R.drawable.back);
/*加载主屏布局*/
setContentView(myLayout);
/*实例化一个LinearLayout布局参数,用来添加View*/
layoutP=newLinearLayout.LayoutParams(WC,WC);
/*构造实例化TextView对象*/
constructTextView();
/*把TextView添加到LinearLayout布局中*/
addTextView();
/*设置TextView文本颜色*/
setTextViewColor();
/*设置TextView文本内容*/
setTextViewText();
}
/*设置TextView文本内容*/
publicvoidsetTextViewText(){
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文本颜色*/
publicvoidsetTextViewColor(){
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对象*/
publicvoidconstructTextView(){
black_TV=newTextView(this);
blue_TV=newTextView(this);
cyan_TV=newTextView(this);
dkgray_TV=newTextView(this);
gray_TV=newTextView(this);
green_TV=newTextView(this);
ltgray_TV=newTextView(this);
magenta_TV=newTextView(this);
red_TV=newTextView(this);
transparent_TV=newTextView(this);
white_TV=newTextView(this);
yellow_TV=newTextView(this);
}
/*把TextView添加到LinearLayout布局中*/
publicvoidaddTextView(){
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);
}
}
3 结果
android.graphics.Typeface实践
字体风格Typeface种类
intStyle类型
BOLD
BOLD_ITALIC
ITALIC
NORMAL
粗体
粗斜体
斜体
普通字体
Typeface类型
DEFAULT
DEFAULT_BOLD
MONOSPACE
SANS_SERIF
SERIF
默认字体
默认粗体
单间隔字体
无衬线字体
衬线字体
Typeface.create(Typefacefamily,intstyle)
创建一个混合型新的字体:
有4*5中搭配
Typeface.setTypeface(Typefacetf,intstyle)
设置一个混合型字体:
有4*5中搭配
Typeface.setTypeface(Typefacetf)
设置一个只有Typeface风格的字体:
有五种形式
编程实现以上静态域字体
1 创建新工程
2 修改mianActivity.java,实现多种字体TextView显示
packagezyf.TypefaceStudy;
/*导入要使用的包*/
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.graphics.Typeface;
importandroid.os.Bundle;
importandroid.view.ViewGroup;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;
publicclassTypefaceStudyextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
/*
*android.graphics.Typefacejava.lang.Object
Typeface类指定一个字体的字体和固有风格.
*该类用于绘制,与可选绘制设置一起使用,
如textSize,textSkewX,textScaleX当绘制(测量)时来指定如何显示文本.
*/
/*定义实例化一个布局大小,用来添加TextView*/
finalintWRAP_CONTENT=ViewGroup.LayoutParams.WRAP_CONTENT;
/*定义TextView对象*/
privateTextViewbold_TV,bold_italic_TV,default_TV,
default_bold_TV,italic_TV,monospace_TV,
normal_TV,sans_serif_TV,serif_TV;
/*定义LinearLayout布局对象*/
privateLinearLayoutlinearLayout;
/*定义LinearLayout布局参数对象*/
privateLinearLayout.LayoutParamslinearLayouttParams;
@Override
publicvoidonCreate(Bundleicicle){
super.onCreate(icicle);
/*定义实例化一个LinearLayout对象*/
linearLayout=newLinearLayout(this);
/*设置LinearLayout布局为垂直布局*/
linearLayout.setOrientation(LinearLayout.VERTICAL);
/*设置布局背景图*/
linearLayout.setBackgroundResource(R.drawable.back);
/*加载LinearLayout为主屏布局,显示*/
setContentView(linearLayout);
/*定义实例化一个LinearLayout布局参数*/
linearLayouttParams=
newLinearLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT);
constructTextView();
setTextSizeOf();
setTextViewText();
setStyleOfFont();
setFontColor();
toAddTextViewToLayout();
}
publicvoidconstructTextView(){
/*实例化TextView对象*/
bold_TV=newTextView(this);
bold_italic_TV=newTextView(this);
default_TV=newTextView(this);
default_bold_TV=newTextView(this);
italic_TV=newTextView(this);
monospace_TV=newTextView(this);
normal_TV=newTextView(this);
sans_serif_TV=newTextView(this);
serif_TV=newTextView(this);
}
publicvoidsetTextSizeOf(){
//设置绘制的文本大小,该值必须大于0
bold_TV.setTextSize(24.0f);
bold_italic_TV.setTextSize(24.0f);
default_TV.setTextSize(24.0f);
default_bold_TV.setTextSize(24.0f);
italic_TV.setTextSize(24.0f);
monospace_TV.setTextSize(24.0f);
normal_TV.setTextSize(24.0f);
sans_serif_TV.setTextSize(24.0f);
serif_TV.setTextSize(24.0f);
}
publicvoidsetTextViewText(){
/*设置文本*/
bold_TV.setText("BOLD");
bold_italic_TV.setText("BOLD_ITALIC");
default_TV.setText("DEFAULT");
default_bold_TV.setText("DEFAULT_BOLD");
italic_TV.setText("ITALIC");
monospace_TV.setText("MONOSPACE");
normal_TV.setText("NORMAL");
sans_serif_TV.setText("SANS_SERIF");
serif_TV.setText("SERIF");
}
publicvoidsetStyleOfFont(){
/*设置字体风格*/
bold_TV.setTypeface(null,Typeface.BOLD);
bold_italic_TV.setTypeface(null,Typeface.BOLD_ITALIC);
default_TV.setTypeface(Typeface.DEFAULT);
default_bold_TV.setTypeface(Typeface.DEFAULT_BOLD);
italic_TV.setTypeface(null,Typeface.ITALIC);
monospace_TV.setTypeface(Typeface.MONOSPACE);
normal_TV.setTypeface(null,Typeface.NORMAL);
sans_serif_TV.setTypeface(Typeface.SANS_SERIF);
serif_TV.setTypeface(Typeface.SERIF);
}
publicvoidsetFontColor(){
/*设置文本颜色*/
bold_TV.setTextColor(Color.BLACK);
bold_italic_TV.setTextColor(Color.CYAN);
default_TV.setTextColor(Color.GREEN);
default_bold_TV.setTextColor(Color.MAGENTA);
italic_TV.setTextColor(Color.RED);
monospace_TV.setTextColor(Color.WHITE);
normal_TV.setTextColor(Color.YELLOW);
sans_serif_TV.setTextColor(Color.GRAY);
serif_TV.setTextColor(Color.LTGRAY);
}
publicvoidtoAddTextViewToLayout(){
/*把TextView加入LinearLayout布局中*/
linearLayout.addView(bold_TV,linearLayouttParams);
linearLayout.addView(bold_italic_TV,linearLayouttParams);
linearLayout.addView(default_TV,linearLayouttParams);
linearLayout.addView(default_bold_TV,linearLayouttParams);
linearLayout.addView(italic_TV,linearLayouttParams);
linearLayout.addView(monospace_TV,linearLayouttParams);
linearLayout.addView(normal_TV,linearLayouttParams);
linearLayout.addView(sans_serif_TV,linearLayouttParams);
linearLayout.addView(serif_TV,linearLayouttParams);
}
}
3 结果
更改手机窗口画面底色
drawable定义颜色常数的方法
1 编写main布局
xmlversion="1.0"encoding="utf-8"?
>
android:
id="@+id/widget0"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
xmlns:
android="
>
android:
id="@+id/name"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="账号"
android:
layout_x="61px"
android:
layout_y="69px"
>
android:
id="@+id/password"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
text="密码"
android:
layout_x="61px"
android:
layout_y="158px"
>
android:
id="@+id/name_in"
android:
layout_width="120dip"
android:
l