Android 基础UI编程 1副本.docx
- 文档编号:5341468
- 上传时间:2023-05-08
- 格式:DOCX
- 页数:15
- 大小:74.41KB
Android 基础UI编程 1副本.docx
《Android 基础UI编程 1副本.docx》由会员分享,可在线阅读,更多相关《Android 基础UI编程 1副本.docx(15页珍藏版)》请在冰点文库上搜索。
Android基础UI编程1副本
Android基础UI编程1
更改与显示文字标签
TextView标签的使用
1 新建工程
2 打开layout包中的activity_main.xml文件,定义一个TextView
3 在MainActivity.java中声明一个TextView,利用findViewById()方法获取activity_main.xml中的TextView
TextViewmTextView01;
mTextView01=(TextView)findViewById(R.id.textView1);
4 增加文本更改监听器
myTextView.addTextChangedListener(newTextWatcher(){//添加监听器
publicvoidafterTextChanged(Editables){//文本改变后
//TODOAuto-generatedmethodstub
}
publicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter){
//TODOAuto-generatedmethodstub//文本改变前
}
publicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){
//TODOAuto-generatedmethodstub//文本改变时
Toast.makeText(getApplicationContext(),
"原字符串共"+String.valueOf(before)+"个字符。
"+"从第"+String.valueOf(start)+"个字符开始更改为字符串:
"+s+"。
共"+String.valueOf(count)+"字符。
",
Toast.LENGTH_LONG).show();//显示提示信息
}
});
mTextView01.setText("欢迎来到Android的TextView世界...");
5
6 设置TextView标签内容
Stringstr_2="欢迎来到Android的TextView世界...";
mTextView01.setText(str_2);
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
黑色
蓝色
青绿色
灰黑色
灰色
绿色
浅灰色
红紫色
红色
透明
白色
黄色
/*设置TextView文本颜色*/
mTextView01.setTextColor(Color.RED);
1 结果
android.graphics.Typeface实践
字体风格Typeface种类
intStyle类型
BOLD
BOLD_ITALIC
ITALIC
NORMAL
粗体
粗斜体
斜体
普通字体
Typeface类型
DEFAULT
DEFAULT_BOLD
MONOSPACE
SANS_SERIF
SERIF
默认字体
默认粗体
单间隔字体
无衬线字体
衬线字体
编程实现以上静态域字体
1 创建新工程
2 修改mianActivity.java,实现多种字体TextView显示
//设置绘制的文本大小,该值必须大于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);
/*设置字体风格*/
.setTypeface(null,Typeface.BOLD);
.setTypeface(null,Typeface.BOLD_ITALIC);
.setTypeface(Typeface.DEFAULT);
.setTypeface(Typeface.DEFAULT_BOLD);
.setTypeface(null,Typeface.ITALIC);
.setTypeface(Typeface.MONOSPACE);
.setTypeface(null,Typeface.NORMAL);
.setTypeface(Typeface.SANS_SERIF);
.setTypeface(Typeface.SERIF);
更改手机窗口画面底色
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: layout_height="wrap_content" android: textSize="18sp" android: layout_x="114px" android: layout_y="57px" > 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" >
2 在values文件夹中定义一个color.xml文件用来存放颜色值
xmlversion="1.0"encoding="utf-8"?
>
3 修改main.xml中的屏幕背景颜色和TextView的字体颜色
android: id="@+id/widget0" android: layout_width="fill_parent" android: layout_height="fill_parent" android: background="@color/white" xmlns: android=" > 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" > 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" > 4 在mainActivity.java代码中修改TextView背景颜色 publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); TextViewtext=(TextView)findViewById(R.id.name); //由ID获得资源 ResourcesmyColor=getBaseContext().getResources(); //getBaseContext()获得基础Context //getResources()获得资源 Drawablecolor_M=myColor.getDrawable(R.color.lightgreen); //由资源myColor来获得DrawableR.color.lightgreen是颜色值的ID引用 text.setBackgroundDrawable(color_M); //设置背景 } 5 结果: 代码中更改TextView文字颜色 1 创建工程 2 编写布局main.xml xmlversion="1.0"encoding="utf-8"? > android=" android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" > android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="@string/hello" /> android: text="TextView01" android: id="@+id/TextView01" android: layout_width="wrap_content" android: layout_height="wrap_content"> android: text="这里使用Graphics颜色静态常量" android: id="@+id/TextView02" android: layout_width="wrap_content" android: layout_height="wrap_content"> 3 新加drawable.xml,其中添加一个white颜色值 xmlversion="1.0"encoding="utf-8"? > 4 在代码中由ID获取TextView TextViewtext_A=(TextView)findViewById(R.id.TextView01); TextViewtext_B=(TextView)findViewById(R.id.TextView02); 5 获取Resources对象 ResourcesmyColor_R=getBaseContext().getResources(); //getBaseContext()获得基础Context //getResources()从Context获取资源实例对象 6 获取Drawable对象 DrawablemyColor_D=myColor_R.getDrawable(R.color.white); 7 设置文本背景颜色 text_A.setBackgroundDrawable(myColor_D); 8 利用android.graphics.Color的颜色静态变量来改变文本颜色 text_A.setTextColor(android.graphics.Color.GREEN); 9 利用Color的静态常量来设置文本颜色 text_B.setTextColor(Color.RED); 取得手机屏幕大小 DisplayMetrics类取得画面宽高 1 创建新工程 2 修改main.xml布局,添加一个TextView xmlversion="1.0"encoding="utf-8"? > android=" android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" > android: text="TextView01" android: id="@+id/TextView01" android: layout_width="wrap_content" android: layout_height="wrap_content"> 3 在代码中定义一个DisplayMetrics类对象 DisplayMetricsdisplaysMetrics=newDisplayMetrics(); //DisplayMetrics一个描述普通显示信息的结构,例如显示大小、密度、字体尺寸 4 获取手机窗口的Display来初始化DisplayMetrics对象 getWindowManager().getDefaultDisplay().getMetrics(displaysMetrics); //getManager()获取显示定制窗口的管理器。 //获取默认显示Display对象 //通过Display对象的数据来初始化一个DisplayMetrics对象 5 得到屏幕宽高 StringshowSize="手机屏幕分辨率: \n"+ displaysMetrics.widthPixels+"*"+displaysMetrics.heightPixels; 6 在mainActivity.java中findViewByID()获取TextView TextViewmyShow=(TextView)findViewById(R.id.TextView01); 7 显示屏幕分辨率信息 myShow.setText(showSize); 样式化的定型对象 Style样式的定义 1 新建工程 2 定义一个style.xml存放样式 xmlversion="1.0"encoding="utf-8"? > textSize">25sp textColor">#80FF00 textSize">18sp textColor">#0C688E fromAlpha">0.0 toAlpha">0.0 3 在string.xml中添加字符串 xmlversion="1.0"encoding="utf-8"? > 4 修改布局main.xml,添加两个TextView android: id="@+id/TextView01" android: layout_height="wrap_content" android: layout_width="fill_parent" android: gravity="center_vertical|center_horizontal" android: text="@string/string_A"> android: id="@+id/TextView02" android: layout_height="wrap_content" android: layout_width="fill_parent" android: gravity="center_vertical|center_horizontal" android: text="@string/string_B"> 5 加入Style xmlversion="1.0"encoding="utf-8"? > xmlns: android=" android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent"> android: id="@+id/TextView01" style="@style/myStyle_Text1" android: layout_height="wrap_content" android: layout_width="fill_parent" android: gravity="center_vertical|center_horizontal" android: text="@string/string_A"> android: id="@+id/TextView02" style="@style/myStyle_Text2" android: layout_height="wrap_content" android: layout_width="fill_parent" android: gravity="center_vertical|center_horizontal" android: text="@string/string_B"> 6 结果:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 基础UI编程 副本 基础 UI 编程