3、android ui编程进阶(教程与案例)共32页
Android_基础UI编程[1]
![Android_基础UI编程[1]](https://img.taocdn.com/s3/m/204668d9d15abe23482f4dcd.png)
-----------------------------------Android 编程基础1封面-----------------------------------Android 编程基础2Android基础UI编程2标题、状态栏的隐藏标题栏隐藏在Activity.setCurrentView();之前调用此方法状态栏隐藏(全屏)在Activity.setCurrentView();之前调用此方法private void HideTitle(){//TODO Auto-generated method stubrequestWindowFeature(Window.FEATURE_NO_TITLE);}private void HideStatusBar(){//TODO Auto-generated method stub//隐藏标题requestWindowFeature(Window.FEATURE_NO_TITLE);//定义全屏参数int flag=youtParams.FLAG_FULLSCREEN;//获得窗口对象Window myWindow=this.getWindow();//设置Flag标识myWindow.setFlags(flag,flag);}-----------------------------------Android3样式化的定型对象Style样式的定义①新建工程②定义一个style.xml存放样式③在string.xml中添加字符串④修改布局main.xml,添加两个TextView<?xml version="1.0"encoding="utf-8"?><resources><style name="myStyle_Text1"><item name="android:textSize">25sp</item><item name="android:textColor">#80FF00</item></style><style name="myStyle_Text2"><item name="android:textSize">18sp</item><item name="android:textColor">#0C688E</item><item name="android:fromAlpha">0.0</item><item name="android:toAlpha">0.0</item></style></resources><?xml version="1.0"encoding="utf-8"?><resources><string name="string_A">应用myStyle_Text1</string><string name="string_B">应用myStyle_Text2</string></resources><TextViewandroid:id="@+id/TextView01"android:layout_height="wrap_content"android:layout_width="fill_parent"android:gravity="center_vertical|center_horizontal"android:text="@string/string_A"></TextView><TextViewandroid:id="@+id/TextView02"android:layout_height="wrap_content"android:layout_width="fill_parent"android:gravity="center_vertical|center_horizontal"android:text="@string/string_B"></TextView>-----------------------------------Android4⑤加入Style⑥结果:<?xml version="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid: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"></TextView><TextViewandroid: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"></TextView></LinearLayout>-----------------------------------Android 编程基础5简易的按钮事件Button事件处理①创建新工程②修改main.xml布局,添加一个TextView和一个Button③在mainActivity.java中findViewByID()获取TextView和Button资源④给Button添加事件监听器Button.OnClickListener()<?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:id="@+id/show_TextView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><Buttonandroid:id="@+id/Click_Button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="点击"/></LinearLayout>show=(TextView)findViewById(R.id.show_TextView);press=(Button)findViewById(R.id.Click_Button);press.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v){//TODO Auto-generated method stub}});-----------------------------------Android 编程基础6⑤处理事件⑥结果:press.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View v){//TODO Auto-generated method stubshow.setText("Hi,Google Android!");}});-----------------------------------Android编程基础7手机页面的转换setContentView的应用①新建工程②string添加两个提示字符串③新建color.xml保存两个颜色值④修改main.xml布局,添加一个TextView和一个Button<?xml version="1.0"encoding="utf-8"?><resources><string name="layout1">this is Layout1</string><string name="layout2">This is Layout2</string><string name="app_name">Ex8_UI</string></resources><?xml version="1.0"encoding="utf-8"?><resources><color name="black">#000000</color><color name="white">#FFFFFFFF</color></resources><?xml version="1.0"encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/black"xmlns:android="/apk/res/android" ><TextViewandroid:id="@+id/text1"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:text="@string/layout1"></TextView><Buttonandroid:id="@+id/button1"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"-----------------------------------Android 编程基础8⑤新建mylayout.xml布局文件,并添加两个View:TextView和Button⑥编写mainActivity.javaandroid:layout_y="82px"android:text="Go to Layout2"></Button></AbsoluteLayout><?xml version="1.0"encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/white"xmlns:android="/apk/res/android"><TextViewandroid:id="@+id/text2"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:textColor="@color/black"android:text="@string/layout2"></TextView><Buttonandroid:id="@+id/button2"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Layout1"></Button></AbsoluteLayout>package zyf.Ex8_UI;import android.app.Activity;/*import相关class*/import android.os.Bundle;import android.view.View;import android.widget.Button;public class Ex8_UI extends Activity{/**Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState){-----------------------------------Android 编程基础9⑦结果super.onCreate(savedInstanceState);/*载入main.xml Layout*/setContentView(yout.main);//默认启动布局/*以findViewById()取得Button对象,并添加onClickListener*/Button b1=(Button)findViewById(R.id.button1);b1.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){jumpToLayout2();//调用跳转方法jumpToLayout2()}});}/*method jumpToLayout2:将layout由main.xml切换成mylayout.xml*/public void jumpToLayout2(){/*将layout改成mylayout.xml*/setContentView(yout.mylayout);/*以findViewById()取得Button对象,并添加onClickListener*/Button b2=(Button)findViewById(R.id.button2);b2.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){jumpToLayout1();//调用跳转方法jumpToLayout1()}});}/*method jumpToLayout1:将layout由mylayout.xml切换成main.xml*/public void jumpToLayout1(){/*将layout改成main.xml*/setContentView(yout.main);/*以findViewById()取得Button对象,并添加onClickListener*/Button b1=(Button)findViewById(R.id.button1);b1.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){jumpToLayout2();//调用跳转方法jumpToLayout2()}});}}-----------------------------------Android 编程基础10调用另一个ActivityIntent对象的使用①新建工程②在string.xml中添加两个字符串③新建color.xml存放颜色值④修改main.xml布局,添加一个TextView和一个Button<?xml version="1.0"encoding="utf-8"?><resources><string name="hello">Hello World,Ex9_UI!</string><string name="app_name">Ex9_UI</string><string name="act1">This is Activity1!</string><string name="act2">This is Activity2!</string></resources><?xml version="1.0"encoding="utf-8"?><resources><color name="black">#000000</color><color name="white">#FFFFFFFF</color></resources><?xml version="1.0"encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/black"xmlns:android="/apk/res/android"><TextViewandroid:id="@+id/text1"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:text="@string/act1"></TextView><Buttonandroid:id="@+id/button1"android:layout_width="118px"android:layout_height="wrap_content"-----------------------------------Android 编程基础11⑤新建一个secondlayout.xml布局,并添加一个TextView和一个Button⑥新建SecondActivity.java文件,添加内容android:layout_x="100px"android:layout_y="82px"android:text="Go to Activity2"></Button></AbsoluteLayout><?xml version="1.0"encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/white"xmlns:android="/apk/res/android"><TextViewandroid:id="@+id/text2"android:textSize="24sp"android:layout_width="186px"android:layout_height="29px"android:layout_x="70px"android:layout_y="32px"android:textColor="@color/black"android:text="@string/act2"></TextView><Buttonandroid:id="@+id/button2"android:layout_width="118px"android:layout_height="wrap_content"android:layout_x="100px"android:layout_y="82px"android:text="Go to Activity1"></Button></AbsoluteLayout>package zyf.Ex9_UI;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class SecondActivity extends Activity{-----------------------------------Android 编程基础12⑦修改mainActivity.java,添加代码/**Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);/*载入mylayout.xml Layout*/setContentView(yout.mylayout);/*以findViewById()取得Button对象,并添加onClickListener*/Button b2=(Button)findViewById(R.id.button2);b2.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){/*new一个Intent对象,并指定要启动的class*/Intent intent=new Intent();intent.setClass(SecondActivity.this,Ex9_UI.class);/*调用一个新的Activity*/startActivity(intent);/*关闭原本的Activity*/SecondActivity.this.finish();}});}}package zyf.Ex9_UI;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Ex9_UI extends Activity{/**Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);/*载入main.xml Layout*/setContentView(yout.main);/*以findViewById()取得Button对象,并添加onClickListener*/Button b1=(Button)findViewById(R.id.button1);b1.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){/*new一个Intent对象,并指定要启动的class*/Intent intent=new Intent();-----------------------------------Android 编程基础13⑧在AndroidManifest.xml文件中添加SecondActivity⑨结果intent.setClass(Ex9_UI.this,SecondActivity.class);/*调用一个新的Activity*/startActivity(intent);/*关闭原本的Activity*/Ex9_UI.this.finish();}});}}<?xml version="1.0"encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="zyf.Ex9_UI"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon"android:label="@string/app_name"><activity android:name=".Ex9_UI"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="UNCHER"/></intent-filter></activity><activity android:name="SecondActivity"></activity></application><uses-sdk android:minSdkVersion="2"/></manifest>-----------------------------------Android 编程基础14不同Activity之间的数据传递Bundle对象的实现①新建工程②修改main.xml布局,添加UI元素<?xml version="1.0"encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="/apk/res/android"><TextViewandroid:id="@+id/showText"android:layout_width="wrap_content"android:layout_height="26px"android:text="计算你的标准体重!"android:textSize="25px"android:layout_x="65px"android:layout_y="21px"></TextView><TextViewandroid:id="@+id/text_Sex"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="性别:"android:layout_x="71px"android:layout_y="103px"></TextView><TextViewandroid:id="@+id/text_Height"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="身高:"android:layout_x="72px"android:layout_y="169px"></TextView><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="wrap_content"android:layout_height="37px"android:orientation="horizontal"-----------------------------------Android 编程基础15③新建mylayout.xml,并添加UI元素android:layout_x="124px"android:layout_y="101px"><RadioButtonandroid:id="@+id/Sex_Man"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"></RadioButton><RadioButtonandroid:id="@+id/Sex_Woman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"></RadioButton></RadioGroup><EditTextandroid:id="@+id/height_Edit"android:layout_width="123px"android:layout_height="wrap_content"android:text=""android:textSize="18sp"android:layout_x="124px"android:layout_y="160px"></EditText><Buttonandroid:id="@+id/button_OK"android:layout_width="80px"android:layout_height="wrap_content"android:text="计算"android:layout_x="125px"android:layout_y="263px"></Button></AbsoluteLayout><?xml version="1.0"encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="/apk/res/android"><TextViewandroid:id="@+id/text1"-----------------------------------Android 编程基础16④新建一个BMIActivity.java⑤在AndroidManifest.xml添加Activity定义android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:layout_x="50px"android:layout_y="72px"></TextView></AbsoluteLayout>package zyf.Ex10_UI;import android.app.Activity;import android.os.Bundle;public class BMIActivity extends Activity{/**Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);}}<?xml version="1.0"encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="zyf.Ex10_UI"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon"android:label="@string/app_name"><activity android:name=".Ex10_UI"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="UNCHER"/></intent-filter></activity><activity android:name="BMIActivity"></activity></application><uses-sdk android:minSdkVersion="2"/></manifest>-----------------------------------Android 编程基础17⑥修改BMIActivity.java内容package zyf.Ex10_UI;/*import相关class*/import java.text.DecimalFormat;import java.text.NumberFormat;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class BMIActivity extends Activity{/**Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);/*加载main.xml Layout*/setContentView(yout.mylayout);/*取得Intent中的Bundle对象*/Bundle bunde=this.getIntent().getExtras();/*取得Bundle对象中的数据*/String sex=bunde.getString("sex");double height=bunde.getDouble("height");/*判断性别*/String sexText="";if(sex.equals("M")){sexText="男性";}else{sexText="女性";}/*取得标准体重*/String weight=this.getWeight(sex,height);/*设置输出文字*/TextView tv1=(TextView)findViewById(R.id.text1);tv1.setText("你是一位"+sexText+"\n你的身高是"+height+"厘米\n你的标准体重是"+weight+"公斤");}/*四舍五入的method*/private String format(double num){NumberFormat formatter=new DecimalFormat("0.00");String s=formatter.format(num);return s;}-----------------------------------Android 编程基础18⑦修改mainActivity.java内容/*以findViewById()取得Button对象,并添加onClickListener*/private String getWeight(String sex,double height){String weight="";if(sex.equals("M")){weight=format((height-80)*0.7);}else{weight=format((height-70)*0.6);}return weight;}}package zyf.Ex10_UI;/*import相关class*/import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;public class Ex10_UI extends Activity{/**Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);/*载入main.xml Layout*/setContentView(yout.main);/*以findViewById()取得Button对象,并添加onClickListener*/Button ok=(Button)findViewById(R.id.button_OK);ok.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){/*取得输入的身高*/EditText et=(EditText)findViewById(R.id.height_Edit);double height=Double.parseDouble(et.getText().toString());/*取得选择的性别*/String sex="";RadioButton rb1=(RadioButton)findViewById(R.id.Sex_Man);if(rb1.isChecked()){sex="M";}else{-----------------------------------Android 编程基础19⑧结果:sex="F";}/*new一个Intent对象,并指定class*/Intent intent=new Intent();intent.setClass(Ex10_UI.this,BMIActivity.class);/*new一个Bundle对象,并将要传递的数据传入*/Bundle bundle=new Bundle();bundle.putDouble("height",height);bundle.putString("sex",sex);/*将Bundle对象assign给Intent*/intent.putExtras(bundle);/*调用Activity EX03_10_1*/startActivity(intent);}});}}-----------------------------------Android 编程基础20返回数据到前一个ActivitystartActivityForResult方法①新建工程②修改main.xml布局,添加UI元素<?xml version="1.0"encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="/apk/res/android"><TextViewandroid:id="@+id/showText"android:layout_width="wrap_content"android:layout_height="26px"android:text="计算你的标准体重!"android:textSize="25px"android:layout_x="65px"android:layout_y="21px"></TextView><TextViewandroid:id="@+id/text_Sex"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="性别:"android:layout_x="71px"android:layout_y="103px"></TextView><TextViewandroid:id="@+id/text_Height"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="身高:"android:layout_x="72px"android:layout_y="169px"></TextView><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="wrap_content"android:layout_height="37px"android:orientation="horizontal"-----------------------------------Android 编程基础21③新建一个mylayout.xml布局,添加UI元素android:layout_x="124px"android:layout_y="101px"><RadioButtonandroid:id="@+id/Sex_Man"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"></RadioButton><RadioButtonandroid:id="@+id/Sex_Woman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"></RadioButton></RadioGroup><EditTextandroid:id="@+id/height_Edit"android:layout_width="123px"android:layout_height="wrap_content"android:text=""android:numeric="decimal"android:textSize="18sp"android:layout_x="124px"android:layout_y="160px"></EditText><Buttonandroid:id="@+id/button_OK"android:layout_width="80px"android:layout_height="wrap_content"android:text="计算"android:layout_x="125px"android:layout_y="263px"></Button></AbsoluteLayout><?xml version="1.0"encoding="utf-8"?><AbsoluteLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="/apk/res/android"><TextViewandroid:id="@+id/text1"-----------------------------------Android 编程基础22④新建一个SecondActivity.java的Activity子类⑤在AndroidManifest.xml中添加SecondActivity这个Activityandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:layout_x="50px"android:layout_y="72px"></TextView><Buttonandroid:id="@+id/button_back"android:layout_width="100px"android:layout_height="48px"android:text="回上一页"android:layout_x="110px"android:layout_y="180px"></Button></AbsoluteLayout>package zyf.Ex11_UI_A;import android.app.Activity;import android.os.Bundle;public class BMIActivity extends Activity{/**Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);}}<?xml version="1.0"encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="zyf.Ex11_UI_A"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon"android:label="@string/app_name"><activity android:name=".Ex11_UI_A"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="UNCHER"/></intent-filter>上一例子中新添加-----------------------------------Android 编程基础23⑥修改mainActivity.java代码</activity><activity android:name="BMIActivity"></activity></application><uses-sdk android:minSdkVersion="2"/></manifest>package zyf.Ex11_UI_A;import android.app.Activity;/*import相关class*/import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.Toast;public class Ex11_UI_A extends Activity{protected int my_requestCode=1550;private EditText edit_height;private RadioButton radiobutton_Man,radiobutton_Woman;/**Called when the activity is first created.*/@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);/*载入main.xml Layout*/setContentView(yout.main);/*以findViewById()取得Button对象,并添加onClickListener*/ Button ok=(Button)findViewById(R.id.button_OK);edit_height=(EditText)findViewById(R.id.height_Edit); radiobutton_Man=(RadioButton)findViewById(R.id.Sex_Man); radiobutton_Woman=(RadioButton)findViewById(R.id.Sex_Woman); ok.setOnClickListener(new Button.OnClickListener(){public void onClick(View v){try{/*取得输入的身高*/double height=Double.parseDouble(edit_height.getText().toString());/*取得选择的性别*/String sex="";if(radiobutton_Man.isChecked()){sex="M";}else{sex="F";}必须在AndroidManifest中注册新的。
AndroidUI界面编程概览

AndroidUI界面编程概览Android UI界面编程常用控件Android UI界面编程概览本文主要对Android UI 用户界面开放的一些基本概念,均不做深入讲解,但可以快速浏览Android开放经常涉及到的一些基本概念和要素。
一、界面元素1. 窗口:Activity应用程序中每个屏幕显示都通过继承和扩展基类Activity实现分类:基本Activity,带内容的Activity(如ListActivity)2. 对话框:Dialog一种显示于Activity之上的界面元素,由Activity管理,用于显示一些临时信息和功能,它比Activity开销小1) 常用对话框:AlertDialog:信息对话框DatePickerDialog,TimePickerDialog:时间日期选择对话框ProgressDialog:进度对话框还可以设置对话框内容为我们自定义的View2) 相关函数:onCreateDialog():创建对话框的实现showDialog():需要显示的时候调用onPrepareDialog():更改已有对话框时调用3. 菜单:Menu一种显示于Activity之上的界面元素,由Activity管理,用于提供用户一些额外的选项和设置1) 常用菜单:Options Menu:按Menu键时从屏幕底部滑出的菜单Context Menu:用户通过在按件上长按调出它Android UI界面编程常用控件Submenu:用于给菜单分类,可被添加到其它菜单上,不能添加到子菜单上2) 相关函数:onCreateOptionMenu():创建菜单(onCreateContextMenu…)onOptionsItemSelected():处理菜单项的单击事件onPrepareOptionsMenu():动态更新4. 可视化控件:ViewView是可视化控件的基类1) 常用控件:TextView, ImageViewButton, CheckBox, RadioButton, ImageButton, ToggleButtonAnalogClock, DigitalClockEditText, List…2) 数据绑定:adapteradapter将控件(如List, GridView)和复杂数据(如数据,键表)绑定起来5. 其它界面元素标题栏二、layout布局的实现设置控件在屏幕上如何排布1. LinearLayout:线性的布局方式最常用,上下或左右的添加控件2. GridView:网格布局控件按照顺序依次填到每个格子里就好了,使界面很整齐3. TableLayout:表格布局以行列形式管理子控件,每行为一个TableRow,TableRow可添加子控件4. AbsoluteLayout:绝对坐标布局可以让子元素指定准确的x/y坐标值,并显示在屏幕上。
3-Android高级UI控件之四

第3章任务4 使用ViewPager2+Fragment实现多界面集成集成后的界面效果任务4使用V i e w P a g e r2+F r a g m e n t实现多界面集成本次任务的要求Ø应知:Ø了解Fragment的基本概念;Ø底部导航栏控件(BottomNavigationView)的使用;Ø了解适配器控件ViewPager2的基本使用方法;Ø应会Ø会创建和使用Fragment;Ø会使用底部导航栏控件;Ø能够综合使用Fragment和ViewPager2实现多界面集成。
任务4 使用V i e w P a g e r 2+F r a g m e n t 实现多界面集成任务4使用V i e w P a g e r2+F r a g m e n t实现多界面集成Ø为了解决不同屏幕分辨率下的UI设计问题,实现动态、灵活的UI设计,Google在Android 3.0(API level 11)中引入了新的API技术——Fragment。
Ø其主要思路是对Activity中的UI组件进行分组和模块化管理,以达到提升代码重用性和改善用户体验的效果,这些分组后的UI组件就是Fragment。
Ø一个Activity中可以包含多个Fragment模块,而同一个Fragment模块也可以被多个Activity使用。
Ø每个Fragment都有自己的布局和生命周期,但因为Fragment必须被嵌入到Activity中使用,因此,Fragment的生命周期是受其宿主Activity的生命周期所控制的。
当Activity暂停时,该Activity中的所有Fragment都会暂停;当Activity被销毁时,该Activity中的所有Fragment都会被销毁。
任务4 使用V i e w P a g e r 2+F r a g m e n t 实现多界面集成ViewPager2任务4使用V i e w P a g e r2+F r a g m e n t实现多界面集成ØViewPager2是用来替换ViewPager的,直接继承ViewGroup,其内部是使用RecyclerView实现。
【AndroidUI设计与开发】...

【AndroidUI设计与开发】...我觉得这个布局界面是整个项目当中实现起来最复杂的地方,但是把思路理清楚了之后又觉得其实也没有那么复杂,详细说一下我实现的步骤: <1> 最外层使用的是RelativeLayout,主要是为了容易摆放底部菜单栏的位置; <2> 然后是一个FrameLayout,主要用来存放显示Fragment的内容,这里的ID取名为frame_content是用来替换Fragment对象的,在后面的代码中会用到; <3> 最下面的底部菜单栏中外层使用了FrameLayout,之所以使用FrameLayout是为了让底部菜单栏中间的按钮也可以摆放进来,实现一种叠加的效果; <4> 里面嵌套了LinearLayout,使用它是为了能够使用layout_weight属性,可以用来更好的摆放按钮,还可以实现自适应屏幕的效果(关于自适应屏幕的内容后面会有专题详细讲解) <5> 最后里面又嵌套了一个FrameLayout,使用它可以很方便的实现图标在上文字在下的效果,最主要的原因是使用它可以很容易的再添加一个消息提醒的小图片(实际的开发中会用到,此项目中没有用到)2、弹出菜单的布局界面,popwindow_layout.xml:1.<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>2.<RelativeLayout xmlns:android="/apk/res/android"3.xmlns:tools="/tools"4.android:layout_width="fill_parent"5.android:layout_height="fill_parent" >6.7.<LinearLayout8.android:layout_width="fill_parent"9.android:layout_height="wrap_content"10.android:layout_alignParentBottom="true"11.android:background="@drawable/popwindow_bg"12.android:orientation="vertical"13.tools:ignore="UselessParent" >14.15.<LinearLayout16.android:layout_width="wrap_content"17.android:layout_height="wrap_content"18.android:layout_gravity="center_horizontal"19.android:layout_marginTop="15dp"20.android:orientation="horizontal" >21.22.<LinearLayout23.android:layout_width="wrap_content"24.android:layout_height="wrap_content"25.android:layout_margin="5dp"26.android:layout_weight="1"27.android:orientation="vertical" >28.29.<ImageView30.android:layout_width="wrap_content"31.android:layout_height="wrap_content"32.android:layout_gravity="center_horizontal"33.android:layout_marginTop="1.0dip"34.android:src="@drawable/popwindow_write_btn" />35.36.<TextView37.android:layout_width="wrap_content"38.android:layout_height="wrap_content"39.android:layout_gravity="center_horizontal"40.android:layout_marginTop="5.0dip"41.android:shadowColor="#ff000000"42.android:shadowDx="1.0"43.android:shadowDy="1.0"44.android:shadowRadius="1.0"45.android:text="说说"46.android:textColor="#ffffffff"47.android:textSize="13.0dip" />48.</LinearLayout>49.50.<LinearLayout51.android:layout_width="wrap_content"52.android:layout_height="wrap_content"53.android:layout_margin="5dp"54.android:layout_weight="1"55.android:orientation="vertical" >56.57.<ImageView58.android:layout_width="wrap_content"59.android:layout_height="wrap_content"60.android:layout_gravity="center_horizontal"61.android:layout_marginTop="1.0dip"62.android:src="@drawable/popwindow_voice_btn" />63.64.<TextView65.android:layout_width="wrap_content"66.android:layout_height="wrap_content"67.android:layout_gravity="center_horizontal"68.android:layout_marginTop="5.0dip"69.android:shadowColor="#ff000000"70.android:shadowDx="1.0"71.android:shadowDy="1.0"72.android:shadowRadius="1.0"73.android:text="语音"74.android:textColor="#ffffffff"75.android:textSize="13.0dip" />76.</LinearLayout>77.78.<LinearLayout79.android:layout_width="wrap_content"80.android:layout_height="wrap_content"81.android:layout_margin="5dp"82.android:layout_weight="1"83.android:orientation="vertical" >84.85.<ImageView86.android:layout_width="wrap_content"87.android:layout_height="wrap_content"88.android:layout_gravity="center_horizontal"89.android:layout_marginTop="1.0dip"90.android:src="@drawable/popwindow_camera_btn" />91.92.<TextView93.android:layout_width="wrap_content"94.android:layout_height="wrap_content"95.android:layout_gravity="center_horizontal"96.android:layout_marginTop="5.0dip"97.android:shadowColor="#ff000000"98.android:shadowDx="1.0"99.android:shadowDy="1.0"100.android:shadowRadius="1.0"101.android:text="照片"102.android:textColor="#ffffffff"103.android:textSize="13.0dip" />104.</LinearLayout>105.106.<LinearLayout107.android:layout_width="wrap_content"108.android:layout_height="wrap_content"109.android:layout_margin="5dp"110.android:layout_weight="1"111.android:orientation="vertical" >112.113.<ImageView114.android:layout_width="wrap_content"115.android:layout_height="wrap_content"116.android:layout_gravity="center_horizontal"117.android:layout_marginTop="1.0dip"118.android:src="@drawable/popwindow_picture_btn" /> 119.120.<TextView121.android:layout_width="wrap_content"122.android:layout_height="wrap_content"123.android:layout_gravity="center_horizontal"124.android:layout_marginTop="5.0dip"125.android:shadowColor="#ff000000"126.android:shadowDx="1.0"127.android:shadowDy="1.0"128.android:shadowRadius="1.0"129.android:text="视频"130.android:textColor="#ffffffff"131.android:textSize="13.0dip" />133.134.<LinearLayout135.android:layout_width="wrap_content"136.android:layout_height="wrap_content"137.android:layout_margin="5dp"138.android:layout_weight="1"139.android:orientation="vertical" >140.141.<ImageView142.android:layout_width="wrap_content"143.android:layout_height="wrap_content"144.android:layout_gravity="center_horizontal" 145.android:layout_marginTop="1.0dip"146.android:src="@drawable/popwindow_sign_btn" /> 147.148.<TextView149.android:layout_width="wrap_content"150.android:layout_height="wrap_content"151.android:layout_gravity="center_horizontal" 152.android:layout_marginTop="5.0dip"153.android:shadowColor="#ff000000"154.android:shadowDx="1.0"155.android:shadowDy="1.0"156.android:shadowRadius="1.0"157.android:text="签到"158.android:textColor="#ffffffff"159.android:textSize="13.0dip" />160.</LinearLayout>161.</LinearLayout>163.164.</RelativeLayout></span>复制代码3、其中一个Fragment布局页面,fragment_1.xml:1.<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>2.<LinearLayout xmlns:android="/apk/res/android"3.android:layout_width="fill_parent"4.android:layout_height="fill_parent" >5.6.<ImageView7.android:id="@+id/imageview"8.android:layout_width="fill_parent"9.android:layout_height="fill_parent"10.android:scaleType="fitCenter"11.android:src="@drawable/xianjian01" >12.</ImageView>13.14.</LinearLayout></span>复制代码4、自定义按钮的资源文件,由于比较简单,就列出其中一个,tab_friendfeed_btn.xml:1.<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>2.<selector xmlns:android="/apk/res/android">3.4.<itemandroid:drawable="@drawable/toolbar_friendfeed_pressed" android:state_selected="true"/>5.<itemandroid:drawable="@drawable/toolbar_friendfeed_normal"/> 6.7.</selector></span>复制代码5、主Activity界面,MainActivity.java:1.<span style="font-size:12px;">package com.yangyu.mycustomtab03;2.3.import android.content.Context;4.import android.graphics.drawable.BitmapDrawable;5.import android.os.Bundle;6.import android.support.v4.app.FragmentActivity;7.import android.support.v4.app.FragmentTransaction;8.import android.view.Gravity;9.import youtInflater;10.import android.view.MotionEvent;11.import android.view.View;12.import android.view.View.OnClickListener;13.import android.view.View.OnT ouchListener;14.import android.widget.FrameLayout;15.import android.widget.ImageView;16.import android.widget.LinearLayout;17.import android.widget.PopupWindow;18.importandroid.widget.PopupWindow.OnDismissListener;19.20.public class MainActivity extends FragmentActivity implements OnClickListener{21.//定义Fragment页面22.private FragmentPage1 fragmentPage1;23.private FragmentPage2 fragmentPage2;24.private FragmentPage3 fragmentPage3;25.private FragmentPage4 fragmentPage4;26.27.//定义布局对象28.private FrameLayout friendfeedFl,myfeedFl,homeFl,moreFl;29.30.//定义图片组件对象31.private ImageView friendfeedIv,myfeedIv,homeIv,moreIv;32.33.//定义按钮图片组件34.private ImageView toggleImageView,plusImageView;35.36.//定义PopupWindow37.private PopupWindow popWindow;38.39.@Override40.protected void onCreate(Bundle savedInstanceState) {41.super.onCreate(savedInstanceState);42.setContentView(yout.activity_main);43.44.initView();45.46.initData();47.48.//初始化默认为选中点击了“动态”按钮49.clickFriendfeedBtn();50.}51.52./**53.* 初始化组件54.*/55.private void initView(){56.//实例化布局对象57.friendfeedFl = (FrameLayout)findViewById(yout_friendfeed);58.myfeedFl = (FrameLayout)findViewById(yout_myfeed);59.homeFl = (FrameLayout)findViewById(yout_home);60.moreFl = (FrameLayout)findViewById(yout_more);61.62.//实例化图片组件对象63.friendfeedIv = (ImageView)findViewById(R.id.image_friendfeed);64.myfeedIv = (ImageView)findViewById(R.id.image_myfeed);65.homeIv = (ImageView)findViewById(R.id.image_home);66.moreIv = (ImageView)findViewById(R.id.image_more);67.68.//实例化按钮图片组件69.toggleImageView = (ImageView)findViewById(R.id.toggle_btn);70.plusImageView = (ImageView)findViewById(R.id.plus_btn);71.}72.73./**74.* 初始化数据75.*/76.private void initData(){77.//给布局对象设置监听78.friendfeedFl.setOnClickListener(this);79.myfeedFl.setOnClickListener(this);80.homeFl.setOnClickListener(this);81.moreFl.setOnClickListener(this);82.83.//给按钮图片设置监听84.toggleImageView.setOnClickListener(this);85.}86.87.@Override88.public void onClick(View v) {89.switch (v.getId()) {90.//点击动态按钮91.case yout_friendfeed:92.clickFriendfeedBtn();93.break;94.//点击与我相关按钮95.case yout_myfeed:96.clickMyfeedBtn();97.break;98.//点击我的空间按钮99.case yout_home:100.clickHomeBtn();101.break;102.//点击更多按钮103.case yout_more:104.clickMoreBtn();105.break;106.//点击中间按钮107.case R.id.toggle_btn:108.clickT oggleBtn();109.break;110.}111.}112.113./**114.* 显示PopupWindow弹出菜单115.*/116.private void showPopupWindow(View parent){117.if (popWindow == null) {youtInflater layoutInflater = (LayoutInflater) getSystemService(YOUT_INFLATER_SERVICE);119.120.View view = layoutInflater.inflate(yout.popwindow_layout,null);121.// 创建一个PopuWidow对象122.popWindow = new PopupWindow(view,youtParams.FILL_PARENT, 200);123.}124.// 使其聚集,要想监听菜单里控件的事件就必须要调用此方法125.popWindow.setFocusable(true);126.// 设置允许在外点击消失127.popWindow.setOutsideTouchable(true);128.// 设置背景,这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景129.popWindow.setBackgroundDrawable(new BitmapDrawable());130.//设置菜单显示的位置131.popWindow.showAsDropDown(parent,Gravity.CENTER, 0);132.133.//监听菜单的关闭事件134.popWindow.setOnDismissListener(new OnDismissListener() {135.@Override136.public void onDismiss() {137.//改变显示的按钮图片为正常状态138.changeButtonImage();139.}140.});141.142.//监听触屏事件143.popWindow.setTouchInterceptor(new OnTouchListener() {144.public boolean onTouch(View view, MotionEvent event) {145.//改变显示的按钮图片为正常状态146.changeButtonImage();147.148.return false;149.}150.});151.}152.153./**154.* 点击了“动态”按钮155.*/156.private void clickFriendfeedBtn(){157.//实例化Fragment页面158.fragmentPage1 = new FragmentPage1();159.//得到Fragment事务管理器160.FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();161.//替换当前的页面162.fragmentTransaction.replace(R.id.frame_content, fragmentPage1);163.//事务管理提交mit();165.166.friendfeedFl.setSelected(true);167.friendfeedIv.setSelected(true);168.169.myfeedFl.setSelected(false);170.myfeedIv.setSelected(false);171.172.homeFl.setSelected(false);173.homeIv.setSelected(false);174.175.moreFl.setSelected(false);176.moreIv.setSelected(false);177.}178.180.* 点击了“与我相关”按钮181.*/182.private void clickMyfeedBtn(){183.// 实例化Fragment页面184.fragmentPage2 = new FragmentPage2();185.// 得到Fragment事务管理器186.FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();187.// 替换当前的页面188.fragmentTransaction.replace(R.id.frame_content, fragmentPage2);189.// 事务管理提交mit();191.192.friendfeedFl.setSelected(false);193.friendfeedIv.setSelected(false);194.195.myfeedFl.setSelected(true);196.myfeedIv.setSelected(true);197.198.homeFl.setSelected(false);199.homeIv.setSelected(false);200.201.moreFl.setSelected(false);202.moreIv.setSelected(false);203.}204.205./**206.* 点击了“我的空间”按钮208.private void clickHomeBtn(){209.// 实例化Fragment页面210.fragmentPage3 = new FragmentPage3();211.// 得到Fragment事务管理器212.FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();213.// 替换当前的页面214.fragmentTransaction.replace(R.id.frame_content, fragmentPage3);215.// 事务管理提交mit();217.218.friendfeedFl.setSelected(false);219.friendfeedIv.setSelected(false);220.221.myfeedFl.setSelected(false);222.myfeedIv.setSelected(false);223.224.homeFl.setSelected(true);225.homeIv.setSelected(true);226.227.moreFl.setSelected(false);228.moreIv.setSelected(false);229.}230.231./**232.* 点击了“更多”按钮233.*/234.private void clickMoreBtn(){235.// 实例化Fragment页面236.fragmentPage4 = new FragmentPage4();237.// 得到Fragment事务管理器238.FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();239.// 替换当前的页面240.fragmentTransaction.replace(R.id.frame_content, fragmentPage4);241.// 事务管理提交mit();243.244.friendfeedFl.setSelected(false);245.friendfeedIv.setSelected(false);246.247.myfeedFl.setSelected(false);248.myfeedIv.setSelected(false);249.250.homeFl.setSelected(false);251.homeIv.setSelected(false);252.253.moreFl.setSelected(true);254.moreIv.setSelected(true);255.}256.257./**258.* 点击了中间按钮259.*/260.private void clickT oggleBtn(){261.showPopupWindow(toggleImageView);262.//改变按钮显示的图片为按下时的状态263.plusImageView.setImageResource(R.drawable.toolbar_ plusback);264.toggleImageView.setImageResource(R.drawable.toolb ar_btn_pressed);265.}266.267./**268.* 改变显示的按钮图片为正常状态269.*/270.private void changeButtonImage(){271.plusImageView.setImageResource(R.drawable.toolbar_ plus);272.toggleImageView.setImageResource(R.drawable.toolb ar_btn_normal);273.}274.275.}</span><span style="font-size:18px;">276.</span>复制代码6、Fragment其中一个页面,FragmentPage1.java:1.package com.yangyu.mycustomtab03;2.3.import android.os.Bundle;4.import android.support.v4.app.Fragment;5.import youtInflater;6.import android.view.View;7.import android.view.ViewGroup;8.9.public class FragmentPage1 extends Fragment{10.11.@Override12.public View onCreateView(LayoutInflater inflater, ViewGroup container,13.Bundle savedInstanceState) {14.15.return inflater.inflate(yout.fragment_1, null);16.}17.}18.复制代码到这里整个项目就基本上讲完了,大家还可以为此项目继续完善下去,实现点击菜单选项后实现的效果以及顶部标题栏的实现。
Android移动开发入门与进阶

本书简介 本书共分16章。
第1章介绍了Android平台的发展情况;第2章~第10章系统地介绍了Android Eclipse集成开发环境,包括开发工具、开发环境搭建、UI组件、任务和进程/线程模型、单元测试、网络通信开发以及硬件开发等;第11章~第15章详细地介绍了5个综合案例,包括实用的RSS阅读器、有趣的Hot Or Not移动客户端等的开发,并给出了详细代码;第16章介绍了Android Market的情况。
本书适合所有有志于从事Android手机操作系统开发并有一定Java程序设计基础的人员参考使用,也可以作为Android手机操作系统开发的培训教材。
目录第1章 Android移动平台概述 1.1 Google Android平台简介 1.2 Android平台所提供的功能 1.3 Android框架简介 1.4 Android SDK简介 1.5 Android与其他主流手机平台的比较 1.6 本章小结 第2章 Android开发工具 2.1 Android模拟器(Emulator) 2.2 Dalvik调试监控服务工具(DDMS,Dalvik Debug Monitor Service) 2.3 Android资源打包工具(Android Asset Packaging Tool) 2.4 Android调试工具(ADB,Android Debug Bridge) 2.5 SQLite数据库 2.6 Traceview工具 2.7 mksdcard卡 2.8 dx工具 2.9 activityCreator工具 2.10 本章小结 第3章 Android环境搭建及HelloWorld实例 3.1 下载并安装JRE 3.2 下载并安装Eclipse集成开发环境 3.3 下载Android SDK 3.4 下载和安装Apache Ant压缩包 3.5 为Eclipse安装Android开发插件 3.6 创建Android工程 3.7 本章小结 40第4章 Android用户界面(UI)组件 4.1 Android用户界面详解 4.2 通用布局(Layout)对象 4.3 数据绑定 4.4 Button和ImageButton类 4.5 Bitmap图片处理类 4.6 Dialog对话框类 4.7 Menu菜单类 4.8 ListView和GridView类 4.9 Animation类 4.10 简单的用户UI交互程序设计 4.11 多个Activity的用户界面设计 4.12 本章小结 第5章 Android控制层开发 5.1 “机器人”也是要“面子”的——Activity 5.2 它知道你在想什么——Intent和Intent Filter 5.3 你的意图它来广播——Broadcast Receiver 5.4 它为你默默守护着——Service 5.5 它能把信件交给你——NotificationManager 5.6 它能随时提醒你——AlarmManager 5.7 本章小结 第6章 Android任务和进程/线程模型 6.1 Activity和任务 6.2 进程和线程模型 6.3 本章小结 第7章 Android持久层开发 7.1 Android SDK持久层组成概述 7.2 SharedPreferences详解 7.3 文件操作详解 7.4 SQLite数据库详解 7.5 ContentProvider详解 7.6 本章小结 第8章 Android单元测试 8.1 Android测试框架简介 8.2 使用Android Instrumentation进行单元测试 8.3 应用Monkey工具进行性能测试 8.4 Android Instrumentation测试指令 8.5 Android Instrumentation测试代码的实现 8.6 本章小结 第9章 Android网络通信开发 9.1 概述 9.2 HTTP应用 9.3 Socket应用 9.4 SMS应用 9.5 本章小结 第10章 Android访问硬件设备API应用 10.1 概述 10.2 Android电话功能开发 10.3 Android的照相机功能开发 10.4 本章小结 第11章 Android实战案例1:好友列表获取 11.1 获取好友列表背景知识介绍 11.2 功能设计 11.3 获取好友列表代码实现与分析 11.4 获取好友列表应用演示 11.5 本章小结 第12章 Android实战案例2:RSS阅读器 12.1 RSS阅读器功能需求 12.2 RSS介绍 12.3 RSS阅读器功能实现 12.4 RSS Reader应用演示 12.5 本章小结 第13章 Android实战案例3:Hot Or Not移动客户端开发 13.1 Hot Or Not移动客户端需求分析 13.2 Hot Or Not客户端代码实现与分析 13.3 本章小结 第14章 Android实战案例4:MP3播放器 14.1 MP3播放器需求分析 14.2 MP3播放器展示 14.3 MP3播放器代码实现与分析 14.4 本章小结 第15章 Android实战案例5:计算器 15.1 计算器功能需求分析 15.2 计算器UI设计 15.3 计算器控制逻辑设计与实现 15.4 计算器演示 15.5 本章小结 第16章 Android真枪实弹 16.1 Android手机最新进展 16.2 Android Market 16.3 签名和发布自己的应用,赚第一桶金 16.4 本章小结 参考文献 下载后 点击此处查看更多内容。
Android平台应用软件开发课件:Android中的UI控件

Android中的UI控件
4.图片控件ImageView ImageView控件常用于显示图片,其图片的来源可以是 资源文件中图片的id号,也可以是Drawable对象或者位图对 象。其常用属性如表4-4所示。
Android中的UI控件
5.单选按钮控件RadioButton RadioButton控件,继承自 poundButton类,位于android.widget包中。 单选按钮通常不单独使用,一般在RadioGroup中声明,用于 提供两个或多个互斥选项,即在一组单选按钮中只能选择一 个。要监听单选按钮状态的更改,需给它所在的RadioGroup 添加setOnCheckedChangeListener()监听器。其常用属性如表 4-5所示。
Android中的UI控件
Android中的UI控件
1.ListView使用ArrayAdapter适配器 分析一下使用的步骤。 (1) 定义一个数组来存放ListView中item的内容。 (2) 通过实现ArrayAdapter的构造函数来创建一个 ArrayAdapter的对象。 (3) 通过ListView的setAdapter()方法绑定ArrayAdapter。
Android中的UI控件
图4-4 模拟下载进度界面
Android中的UI控件
我们做Android开发时要尽量避免让主线程执行耗时的 操作,例如网络请求、数据库查询等操作,因为在主线程中 执行这些长时间的操作会阻塞主线程,从用户的角度来看, 好像该应用程序出现了死机状态。甚至更糟糕的是,如果 UI程序被阻塞几秒后,就会弹出程序无响应(ANR: Application Not Responding)对话框,如图4-5所示。
Android应用开发案例教程(Android Studio版)

Gradle是一种依赖管理工具 基于Groovy的内部领域特定(DSL)语言
3
资源目录res及其资源类型
• res目录用于存放项目所需要的声音、图片、用户界面等资源文件。其中最重要的三个目录: 1. drawable目录存放图标资源 2. layout目录存放用户界面布局文件。 3. values目录存放参数描述文件资源,都是XML文件,如字符串string.xml、颜色color.xml、 数组arrays.xml等。
Android 系统为开发人员提供了丰富多彩的用户界面组件,大多数组件在 widget 包。
1.3.2 View 类
• 视图组件View类,是用户界面组件的共同父类。几乎所有的高级UI组件都继 承自View类。例如TextView、Button、List,EditText、RadioButton、 Checkbox 等。
android:collapseColumns: 设置需要被隐藏的列序号 android:shrinkColumns: 设置允许被收缩的列序号 android:stretchColumns: 设置运行被拉伸的列序号
设计一个3行4列的表格布局
4 相对布局RelativeLayout
• 相对布局是采用相对其它组件的位置的布局方式。通过指定ID关联其他组件,与之右对齐、 上下对齐等方式来排列组件。 【例1-4】应用相对布局设计一个图片和4个按钮,如下图
布局的控件层级关系和属性
图1-11 帧布局示例结果
3 表格布局TableLayout
• 表格布局将页面划分成行列构成的单元格。由根元素TableLayout来标识表格布局。 • 行由<TableRow></TableRow>定义。 • 组件放置到哪一列,由 android:layout_column 指定列编号。 • 三个常用属性
android ui设计

案例1 案例
学习目标
Activity的主要作用 创建一个Activity的方法 在AndroidManifest.xml文件当中注册应用 Activity的方法 在Activity中查找控件(TextView Button) 对控件进行事件处理
在布局文件中引用颜色资源、 在布局文件中引用颜色资源、字符串资源和尺寸资源
实验任务
完成一个简易计算器,功能包括用户输入两个数 字,点击加减乘除任意一个按钮后显示结果。
训练目标
掌握UI界面的嵌套布局 EditText通过三种属性可以指定
android:digits 数字 0~9 或 字母 a~z android:inputType 自定义输入的类型 android:numeric 数字类型
px (pixels)像素 一般HVGA代表320x480像素,这个用的比较多。 dip或dp (device independent pixels)设备独立像素 这个和设备硬件有关,一般为了支持WVGA、HVGA和QVGA 推荐使用这 个,不依赖像素。 sp (scaled pixels — best for text size)比例像素 主要处理字体的大小,可以根据系统的字体自适应。 除了上面三个显示单位,下面还有几个不太常用: in (inches)英寸 mm (millimeters)毫米 pt (points)点,1/72英寸 为了适应不同分辨率,不同的像素密度,推荐使用dip ,文字使用sp。
MVC设计模式的运用 Menu组件的使用 AlertDialog.Builder对话框的使用
内部类 匿名内部类
案例2 案例
案例2 案例