1、参考下图中界面控件的摆放位置,分别使用线性布局、相对布局和绝对布局实现用户界面,并对比各种布局实现的复杂程度和对不同屏幕尺寸的适应能力。线性布局:main.xml 相对布局main.xmlEditText android:id=+id/Name android:layout_width=fill_parent android:layout_height=wrap_content android:layout_toRightOf=id/NameTextString.xml 姓名: 年龄: 身高: 添加数据 全部显示 清除显示 全部清除 king 30 1.75 RelativeLayout绝对布
2、局main.xml 如果在这个基础上加上TabHost组件,如下图TabDemo.javapublic class TabDemo extends TabActivity public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); TabHost tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.tab1, tabHost.getTabContentView(),true); LayoutInflater
3、.from(this).inflate(R.layout.tab2, tabHost.getTabContentView(),true); LayoutInflater.from(this).inflate(R.layout.tab3, tabHost.getTabContentView(),true); tabHost.addTab(tabHost.newTabSpec(TAB1). setIndicator(线性布局).setContent(R.id.layout01); tabHost.addTab(tabHost.newTabSpec(TAB2). setIndicator(绝对布局)
4、.setContent(R.id.layout02); tabHost.addTab(tabHost.newTabSpec(TAB3). setIndicator(相对布局).setContent(R.id.layout03); 上面是java文件代码,这样还不行,要建立三个xml文件,分别为tab1.xml,tab2.xml,tab3.mxl,也就是上面三种布局的xml文件,不过要在每个xml文件的开头分别加上android:id = +id/layout01,android:id = +id/layout02,android:id = +id/layout03如:tab1.xml(线性)L
5、inearLayout xmlns:android=android:id = +id/layout01ListView的使用Main.xmlListViewDemo.javapublic class ListViewDemo extends Activity /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main
6、); final TextView textView = (TextView)findViewById(R.id.TextView01); ListView listView = (ListView)findViewById(R.id.ListView01); List list = new ArrayList(); list .add(ListView子项1); list .add(ListView子项2); list .add(ListView子项3); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simpl
7、e_list_item_1, list ); listView.setAdapter(adapter);AdapterView.OnItemClickListener listViewListener = new AdapterView.OnItemClickListener()Overridepublic void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) String msg = 父View:+arg0.toString()+n+子View:+arg1.toString()+n+位置:+String.valu
8、eOf(arg2)+,ID:+String.valueOf(arg3);textView.setText(msg);listView.setOnItemClickListener(listViewListener); Spinner应用Main.xmlSpinnerActivity.javapublic class SpinnerActivity extends Activity public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.
9、main); Spinner spinner = (Spinner)findViewById(R.id.spinner);ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.appList,android.R.layout.simple_spinner_item);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); OnItemSelectedL
10、istener itemSelectedListener= new OnItemSelectedListener() public void onItemSelected(AdapterView parent, View view, int position, long id) Toast.makeText(SpinnerActivity.this,parent.getItemAtPosition(position).toString()+被选中, Toast.LENGTH_LONG).show(); Overridepublic void onNothingSelected(AdapterV
11、iew arg0) Toast.makeText(SpinnerActivity.this,Nothing is selected, Toast.LENGTH_LONG).show(); spinner.setOnItemSelectedListener(itemSelectedListener); 表格布局:Main.xml TableLayout android:id=+id/TableLayout01 android:layout_width=fill_parent android:layout_height=fill_parent xmlns:android= 功能描述:主界面上有一个
12、“登录”按钮,点击“登录”按钮后打开一个新的Activity;新的Activity上面有输入用户名和密码的控件,在用户关闭这个Activity后,将用户输入的用户名和密码传递到主界面中。 编程要点:主界面的Activity命名为MainActivity;启动新的Activity命名为UserLoginActivity;分别使用显示启动和隐式启动的方式,启动新的Activity;用户名中不能出现“”符号,同时长度不超过12个字符;密码使用密码文本显示方式,即显示为“*”,同时只能为数字; 返回的用户名和密码要以Toast的方式显示出来; MainActivity和UserLoginActivit
13、y中各个生命周期的回调函数中要以Log.i方式显示日志信息。两个布局xml代码类似上面的布局,故省略,自己模仿MainActivity.javapublic class MainActivity extends Activity protected int SUBACTIVITY1 = 1;/唯一标示子activity标识码private static String TAG = MainActivity;/用来在运行时显示activity日志,即上面要求的第五条Button button1,button2;public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);Log.i(TAG, (1) onCreate();/日志上显示onCreate()button1 = (Button) this.findViewById(R.id.button1);button2 = (Button) this.findViewById(R.id.button2);