布局主要在xml文件里书写,控件总多:
①textView
<TextView
android:id="....
...
/>
其中所有控件都最主要的有id、layout_height、layout_width这几个属性。
对于textView而言,还可以通过gravity改变字的位置,textSize改变字体大小,textColor改变字体颜色等
②Button
其基本布局与textView一致,但可以在activity的java文件里编写监听器,以响应按钮:
button = (Button) findViewById(R.id.button); @Override public void onClick(View v){ switch (v.getId()){ case R.id.button: ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("This is ProgressDialog"); progressDialog.setMessage("Loading..."); progressDialog.setCancelable(true); progressDialog.show(); break; default: break; } }其中findViewById是用来关联控件。onClick方法中swich选择,case哪个控件便执行相应代码。
③editText可输入
1)其中可以用hint属性来提示输入
2)可以用android:maxLines来设定文本框的最大大小
3)可用findViewById关联控件后,读取输入的信息存储。此时可以比如设置②中的响应,在case button后toast存储的信息。
④imageView
通过android:src="..."来引用一个“地址”,从而引用图片。
⑤progessBar来展示进度
1)android:visibility来选定可见度:visible(可见,默认属性);invisible(透明,占据原来位置);gone(不可见,且不占据空间)
2)可通过style来设定进度条类型(水平、圆形等),比如:
style="?android:attr/progressBarStyleHorizontal" 3)还可以通过代码来控制,比如使按一次按钮便增加10%进度: @Override public void onClick(View v) { <span style="white-space:pre"> </span>switch (v.getId()) { <span style="white-space:pre"> </span>case R.id.button: <span style="white-space:pre"> </span>int progress = progressBar.getProgress(); <span style="white-space:pre"> </span>progress = progress + 10; <span style="white-space:pre"> </span>progressBar.setProgress(progress); <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>default: <span style="white-space:pre"> </span>break; } } ⑥AlertDialog弹出对话框主要通过代码实现(即弹出这个行为是一种方法):
@Override public void onClick(View v) { <span style="white-space:pre"> </span>switch (v.getId()) { <span style="white-space:pre"> </span>case R.id.button: <span style="white-space:pre"> </span>AlertDialog.Builder dialog = new AlertDialog.Builder (MainActivity.this); <span style="white-space:pre"> </span>dialog.setTitle("This is Dialog"); <span style="white-space:pre"> </span>dialog.setMessage("Something important."); <span style="white-space:pre"> </span>dialog.setCancelable(false); <span style="white-space:pre"> </span>dialog.setPositiveButton("OK", new DialogInterface. OnClickListener() { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onClick(DialogInterface dialog, int which) { <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>dialog.setNegativeButton("Cancel", new DialogInterface. OnClickListener() { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onClick(DialogInterface dialog, int which) { <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>dialog.show(); <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>default: <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>} }⑦ProgressDialog
与AlertDia用法类似。
更多属性、用法,可查询官方API说法。
