`

Android 基础UI编程1

阅读更多
Android 基础UI编程
更改与显示文字标签
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颜色变幻
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);
}
}

android.graphics.Typeface实践
字体风格Typeface 种类
int Style 类型
Typeface 类型
Typeface.create(Typeface family,int style)
创建一个混合型新的字体:有4*5 中搭配
Typeface.setTypeface (Typeface tf, int style)
设置一个混合型字体:有4*5 中搭配
Typeface.setTypeface(Typeface tf)
设置一个只有Typeface 风格的字体:有五种形式
BOLD           粗体
BOLD_ITALIC    粗斜体 
ITALIC         斜体
NORMAL         普通字体
DEFAULT        默认字体 
DEFAULT_BOLD   默认粗体
MONOSPACE      单间隔字体
SANS_SERIF     无衬线字体
SERIF          衬线字体
编程实现以上静态域字体
① 创建新工程
② 修改mianActivity.java,实现多种字体TextView 显示
package zyf.TypefaceStudy;
/*导入要使用的包*/
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TypefaceStudy extends Activity {
/** Called when the activity is first created. */
/*
* android.graphics.Typeface java.lang.Object
Typeface类指定一个字体的字体和固有风格.
* 该类用于绘制,与可选绘制设置一起使用,
如textSize, textSkewX, textScaleX 当绘制(测量)时来指定如何显示文本.
*/
/* 定义实例化一个布局大小,用来添加TextView */
final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
/* 定义TextView对象*/
private TextView bold_TV, bold_italic_TV, default_TV,
default_bold_TV,italic_TV,monospace_TV,
normal_TV,sans_serif_TV,serif_TV;
/* 定义LinearLayout布局对象*/
private LinearLayout linearLayout;
/* 定义LinearLayout布局参数对象*/
private LinearLayout.LayoutParams linearLayouttParams;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/* 定义实例化一个LinearLayout对象*/
linearLayout = new LinearLayout(this);
/* 设置LinearLayout布局为垂直布局*/
linearLayout.setOrientation(LinearLayout.VERTICAL);
/*设置布局背景图*/
linearLayout.setBackgroundResource(R.drawable.back);
/* 加载LinearLayout为主屏布局,显示*/
setContentView(linearLayout);
/* 定义实例化一个LinearLayout布局参数*/
linearLayouttParams =
new LinearLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT);
constructTextView();
setTextSizeOf();
setTextViewText() ;
setStyleOfFont();
setFontColor();
toAddTextViewToLayout();
} public void constructTextView() {
/* 实例化TextView对象*/
bold_TV = new TextView(this);
bold_italic_TV = new TextView(this);
default_TV = new TextView(this);
default_bold_TV = new TextView(this);
italic_TV = new TextView(this);
monospace_TV=new TextView(this);
normal_TV=new TextView(this);
sans_serif_TV=new TextView(this);
serif_TV=new TextView(this);
} public void setTextSizeOf() {
// 设置绘制的文本大小,该值必须大于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);
} public void setTextViewText() {
/* 设置文本*/
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");
} public void setStyleOfFont() {
/* 设置字体风格*/
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);
} public void setFontColor() {
/* 设置文本颜色*/
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);
} public void toAddTextViewToLayout() {
/* 把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);
}
}

更改手机窗口画面底色
drawable 定义颜色常数的方法
① 编写main 布局
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:layout_x="61px"
android:layout_y="69px"
> </TextView>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:layout_x="61px"
android:layout_y="158px"
> </TextView>
<EditText
android:id="@+id/name_in"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="114px"
android:layout_y="57px"
> </EditText>
<EditText
android:id="@+id/pwd_in"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:password="true"
android:layout_x="112px"
android:layout_y="142px"
> </EditText>
</AbsoluteLayout>

② 在values 文件夹中定义一个drawable.xml 文件用来存放颜色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="darkgray">#938192</color>
<color name="lightgreen">#7cd12e</color>
</resources>

③ 修改main.xml 中的屏幕背景颜色和TextView 的字体颜色
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
android:textColor="@color/darkgray"
android:layout_x="61px"
android:layout_y="69px"
> </TextView>
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textColor="@color/darkgray"
android:layout_x="61px"
android:layout_y="158px"
>

④ 在mainActivity.java 代码中修改TextView 背景颜色
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text=(TextView)findViewById(R.id.name);
//由ID获得资源
Resources myColor=getBaseContext().getResources();
//getBaseContext()获得基础Context
//getResources()获得资源
Drawable color_M=myColor.getDrawable(R.color.lightgreen );
//由资源myColor来获得Drawable R.color.lightgreen是颜色值的ID引用
text.setBackgroundDrawable(color_M);
//设置背景
}

代码中更改TextView 文字颜色
① 创建工程
② 编写布局main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:text="TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="这里使用Graphics颜色静态常量"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>

③ 新加drawable.xml,其中添加一个white 颜色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffffff</color>
</resources>

④ 在代码中由ID 获取TextView
TextView text_A=(TextView)findViewById(R.id.TextView01);
TextView text_B=(TextView)findViewById(R.id.TextView02);

⑤ 获取Resources 对象
Resources myColor_R=getBaseContext().getResources();
//getBaseContext()获得基础Context
//getResources()从Context 获取资源实例对象

⑥ 获取Drawable 对象
Drawable myColor_D=myColor_R.getDrawable(R.color.white);

⑦ 设置文本背景颜色
text_A.setBackgroundDrawable(myColor_D);

⑧ 利用android.graphics.Color 的颜色静态变量来改变文本颜色
text_A.setTextColor(android.graphics.Color.GREEN);

⑨ 利用Color 的静态常量来设置文本颜色
text_B.setTextColor(Color.RED);


置换TextView 文字
CharSequence 数据类型与Resource ID 应用
① 创建新工程
② 修改main.xml 布局,新增一个TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="@string/str_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/myTextView2"
/>
</LinearLayout>

③ 在string.xml 中添加字符串str_2:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Ex4_UI!</string>
<string name="app_name">Ex4_UI</string>
<string name="str_2">我是Resource里的&quot;字符串&quot;</string>
</resources>

④ 在mainActivity.java 中findViewByID()获取TextView
TextView myText=(TextView)findViewById(R.id.myTextView2);

⑤ getString(R.string.str_2)得到原字符串
CharSequence string2=getString(R.string.str_2);

⑥ 定义新字符串str_3
String str_3="我是程序里调用Resource 的";

⑦ 两个字符串连接并重新设置TextView 内容
myText.setText(str_3+string2);


取得手机屏幕大小
DisplayMetrics 类取得画面宽高
① 创建新工程
② 修改main.xml 布局,添加一个TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>

③ 在代码中定义一个DisplayMetrics 类对象
DisplayMetrics displaysMetrics=new DisplayMetrics();
//DisplayMetrics 一个描述普通显示信息的结构,例如显示大小、密度、字体尺寸

④ 获取手机窗口的Display 来初始化DisplayMetrics 对象
getWindowManager().getDefaultDisplay().getMetrics(displaysMetrics);
//getManager()获取显示定制窗口的管理器。
//获取默认显示Display对象
//通过Display 对象的数据来初始化一个DisplayMetrics 对象

⑤ 得到屏幕宽高
String showSize="手机屏幕分辨率: \n"+
displaysMetrics.widthPixels+"*"+displaysMetrics.heightPixels;

⑥ 在mainActivity.java 中findViewByID()获取TextView
TextView myShow=(TextView)findViewById(R.id.TextView01);

⑦ 显示屏幕分辨率信息
myShow.setText(showSize);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics