Android4开发入门经典 之 第四部分:用户界面

基本概念

Android4开发入门经典 之 第四部分:用户界面

常用Widgets和布局

常用的Widgets

TextView、Button、 EditText、CheckBox、RadioGroup、RadioButton、Spinner、ProgressBar、ImageView、Date Picker、Time Picker、Rating Bar、Gallery 、Auto Complete …… 

常用的Layout

Linear Layout、Relative Layout、Table Layout、Grid View、Tab Layout、List View
 

Android常用的Widgets的属性大同小异,这里以TextView为例来看一看

1:android:autoLink
设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)
2:android:autoText
如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。
3:android:bufferType
指定getText()方式取得的文本类别。选项editable 类似于StringBuilder可追加字符,也就是说可调用append方法设置文本内容。spannable 则可在给定的字符区域使用样式,例如:

java代码:
  1. TextView tv = new TextView(this);  
  2. SpannableStringBuilder style=new SpannableStringBuilder("这是测试111");  
  3. style.setSpan(new ForegroundColorSpan(Color.RED), 02, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  4. style.setSpan(new ForegroundColorSpan(Color.YELLOW), 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  5. style.setSpan(new ForegroundColorSpan(Color.BLUE), 46, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  6. tv.setText(style);  
4:android:capitalize
设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到,参见EditView此属性说明。
5:android:cursorVisible
设定光标为显示/隐藏,默认显示。
6:android:digits
设置允许输入哪些字符。如“1234567890.+-*/% ()”
7:android:drawableBottom
在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。
8:android:drawableLeft
在text的左边输出一个drawable,如图片。
9:android:drawablePadding
设置text与drawable(图片)的间隔,与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。
10:android:drawableRight
在text的右边输出一个drawable。
11:android:drawableTop
在text的正上方输出一个drawable。
12:android:editable
设置是否可编辑。
13:android:editorExtras
设置文本的额外的输入数据。
14:android:ellipsize
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—-省略前面显示不了的数据;”end” ——省略后面显示不了的数据;”middle”—-省略中间显示不了的数据;”marquee” ——以跑马灯的方式显示(动画横向移动)
15:android:freezesText
设置保存文本的内容以及光标的位置。
16:android:gravity
设置文本位置,如设置成“center”,文本将居中显示。
17:android:hintText
为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。此属性在 EditView中使用,但是这里也可以用。
18:android:imeOptions
附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditView中使用,此处无用。
19:android:imeActionId
设置IME动作ID。
20:android:imeActionLabel
设置IME动作标签。
21:android:includeFontPadding
设置文本是否包含顶部和底部额外空白,默认为true。
22:android:inputMethod
为文本指定输入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin。
23:android:inputType
设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中使用,这里无效果。
24:android:linksClickable
设置链接是否点击连接,即使设置了autoLink。
25:android:marqueeRepeatLimit
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为 marquee_forever时表示无限次。设置跑马灯的效果
android:singleLine="true"
android:focusable="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
26:android:ems
设置TextView的宽度为N个字符的宽度。这里测试为一个汉字字符宽度
27:android:maxEms
设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。
28:android:minEms
设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。
29:android:maxLength
限制显示的文本长度,超出部分不显示。
30:android:lines
设置文本的行数,设置两行就显示两行,即使第二行没有数据。
31:android:maxLines
设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。
32:android:minLines
设置文本的最小行数,与lines类似。
33:android:lineSpacingExtra
设置行间距。
34:android:lineSpacingMultiplier
设置行间距的倍数。如”1.2”
35:android:numeric
如果被设置,该TextView有一个数字输入法。此处无用,设置后唯一效果是TextView有点击效果,此属性在EdtiView将详细说明。
36:android:password
以小点”.”显示文本
37:android:phoneNumber
设置为电话号码的输入方式。
38:android:privateImeOptions
设置输入法选项,此处无用,在EditText将进一步讨论。
39:android:scrollHorizontally
设置文本超出TextView的宽度的情况下,是否出现横拉条。
40:android:selectAllOnFocus、
如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。 TextView中设置后无效果。
41:android:shadowColor
指定文本阴影的颜色,需要与shadowRadius一起使用。
41:android:shadowDx
设置阴影横向坐标开始位置。
42:android:shadowDy
设置阴影纵向坐标开始位置。
43:android:shadowRadius
设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。
44:android:singleLine
设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:text=“test_ singleLine “,android:singleLine=”true” android:layout_width=“20dp”将只显示“t…”。如果不设置singleLine或者设置为false,文本将自动换行
45:android:text  设置显示文本.
46:android:textAppearance
设置文字外观。如 “?android:attr/textAppearanceLargeInverse”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse
47:android:textColor
设置文本颜色
48:android:textColorHighlight
被选中文字的底色,默认为蓝色
49:android:textColorHint
设置提示信息文字的颜色,默认为灰色。与hint一起使用。
50:android:textColorLink
文字链接的颜色.
51:android:textScaleX
设置文字之间间隔,默认为1.0f。
52:android:textSize
设置文字大小,推荐度量单位”sp”,如”15sp”
53:android:textStyle
设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开
54:android:typeface
设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3]
55:android:height
设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)
56:android:maxHeight
设置文本区域的最大高度
57:android:minHeight
设置文本区域的最小高度
58:android:width
设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),与layout_width 的区别看这里。
59:android:maxWidth
设置文本区域的最大宽度
60:android:minWidth
设置文本区域的最小宽度

Button

正常Button,同以前使用一样。只不过注册事件的时候,还可以在布局文件里面直接注册事件处理,形如:android:OnClick=“testMethod”,那么运行的时候,会调用public void testMethod(View view){ }的方法

Small的Button

只需要在设置中添加style= “?android:attr/buttonStyleSmall“ ,就可以得到比正常情况小的Button
ToggleButton,就是点击,状态会交替出现的Button

java代码:
  1. <ToggleButton  
  2.   android:textOn="设置选定状态的显示文本"  
  3.   android:textOff="设置未选状态的显示文本"  
  4.   ......  
  5.   />  
在程序里面可以通过isChecked方法来判断当前是否选中的状态

java代码:
  1. ImageButton  
  2. <ImageButton  
  3. android:src="@drawable/myimg" ......  
  4. />  

EditText

限制输入框内容的属性,在layout中配置信息
1:android:digits="1234567890.+-*/%()“
限制输入框中只能输入自己定义的这些字符串 如果输入其它将不予以显示
2:android:phoneNumber="true“ 限制输入框中只能输入手机号码
3:android:password="true“限制输入框中输入的任何内容将以“*”符号来显示
4:android:hint=“默认文字“ 输入内容前默认显示在输入框中的提示文字
5:android:textColorHint="#FF0000“设置文字内容颜色
6:android:enabled="false“设置输入框不能被编辑
7:android:maxLength=“10“ 控制输入的最大长度
8:android:numeric 控制输入的数字类型
selectAll()方法是内容全选,getSelectionStart()和getSelectionEnd()可以用于获取选中的内容
可以通过设置EditText的高度来模拟TextArea

Checkbox、RadioGroup和RadioButton

配置的示例如下


java代码:
  1. <RadioGroup  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:orientation="vertical"  
  5.     android:checkedButton="@+id/mycolor"  
  6.     android:id="@+id/menu">  
  7.     <RadioButton  
  8.         android:text="red"  
  9.         android:id="@+id/red"  
  10.         />  
  11.     <RadioButton  
  12.         android:text="green"  
  13.         android:id="@+id/green" />  
  14.     <RadioButton  
  15.         android:text="blue"  
  16.         android:id="@+id/blue" />  
  17.     <TextView  
  18.         android:text="请选择颜色"  
  19.         android:id="@+id/choice" />  
  20. </RadioGroup>  

Spinner

配置的示例如下


java代码:
  1. <Spinner android:id="@+id/spinner"  
  2.   android:layout_width="match_parent"  
  3.   android:layout_height="wrap_content"  
  4.   android:drawSelectorOnTop="true"  
  5.   android:prompt="@string/select_color"  
  6. />  

准备数据,如果是固定的数据,可以在string下面新建一个arrays.xml,内容配置的示例如下:


java代码:
  1. <resources>  
  2.   <string-array name="colors">  
  3.     <item>red</item>  
  4.     <item>green</item>  
  5.     <item>blue</item>  
  6.   </string-array>  
  7. </resources>  

在程序里面把数据设置到Spinner,示例代码如下:


java代码:
  1. final Spinner sp = (Spinner)this.findViewById(R.id.spinner);  
  2. //准备数据  
  3. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(  
  4.         this, R.array.colors, android.R.layout.simple_spinner_item);  
  5. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  6. //把数据设置到spinner  
  7. sp.setAdapter(adapter);  
为Spinner设置事件处理,调用setOnItemSelectedListener方法来设置
要指定默认的选项,可以使用setSelection方法

如果是动态的数据,可以不用定义arrys.xml,而完全用程序设置,示例代码:


java代码:
  1. ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item);   
  2. adapter.add("red");  
  3. adapter.add("blue");  
如果要动态的修改数据,可以使用ArrayAdapter的add和remove方法,注意:固定数据是不可以修改的,也就是不支持这些方法
在事件处理里面,会直接传入被选中项的位置,也就是索引
如果要取得被选中项的文本,可以使用如下代码:

java代码:
  1. sp.getSelectedItem().toString()  

ProgressBar

基本的进度条,直接在布局文件里面配置,示例如下:


java代码:
  1. <ProgressBar  
  2. android:id="@+id/progress_horizontal"  
  3. style="?android:attr/progressBarStyleHorizontal"  
  4. android:layout_width="200dip"  
  5. android:layout_height="wrap_content"  
  6. android:max="100"  
  7. android:progress="10"  
  8. android:secondaryProgress="20" />  
可以通过设置incrementProgressBy()方法来增加进度,incrementProgressBy()方法来减少进度;当然对应的也有incrementSecondaryProgressBy()方法和incrementSecondaryProgressBy()方法。 增加设置正数,减少设置负数。
当然也可以直接使用setProgress和setSecondaryProgress方法来改变进度
可以通过修改style来显示不同的进度样式,具体的Style可以从文档得到

ImageView

主要是在布局文件里面配置,常见属性如下:

1:src:设置要展示的图片
2:adjustViewBounds:调整边框时是否保持可绘制对象的宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。比如想设置图片固定大小,又想保持图片宽高比,需要如下设置:
1) 设置setAdjustViewBounds为true;
2) 设置maxWidth、MaxHeight;
3) 设置设置layout_width和layout_height为wrap_content。
3:scaleType:设置图片的填充方式。 有如下可取的值:
1)matrix :用矩阵来绘图
2)fitXY:拉伸图片(不按比例)以填充View的宽高
3)fitStart :按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边
4)fitCenter :按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间
5)fitEnd :按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的右边
6)center :按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示
7)centerCrop :按比例放大原图直至等于某边View的宽高显示。
8)centerInside  :当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。

DatePicker

DatePicker组件通常是通过Dialog来显示的,所以在需要DatePicker的地方,需要调用显示Dialog的方法,也就是showDialog(int)方法

在创建Dialog的回调方法中,去真正创建DatePickerDialog,示例代码如下:


java代码:
  1. protected Dialog onCreateDialog(int id) {  
  2.   switch (id) {  
  3.   case 1:  
  4.       return new DatePickerDialog(this,  
  5.                   mDateSetListener,  
  6.                   mYear, mMonth, mDay);  
  7.   }  
  8.   return null;  
  9. }  
注意:上面代码里面的mDateSetListener为OnDateSetListener的实现,在里面处理日期选择后的后续处理。

TimePicker

TimePicker的使用和DatePicker的使用大同小异,也是在需要TimePicker的地方,通常需要调用显示Dialog的方法,也就是showDialog(int)方法

在创建Dialog的回调方法中,去真正创建TimePickerDialog,示例代码如下:


java代码:
  1. protected Dialog onCreateDialog(int id) {  
  2.   switch (id) {  
  3.   case TIME_DIALOG_ID:  
  4.       return new TimePickerDialog(this,  
  5.               mTimeSetListener, mHour, mMinute, false);  
  6.   }  
  7.   return null;  
  8. }  
注意:
1:上面代码里面的mTimeSetListener为OnTimeSetListener的实现,在里面处理日期选择后的后续处理。
2:TimePicker只有时和分,没有秒,后面一个boolean参数表示是否24小时

RatingBar

RatingBar的使用非常简单,在布局文件里面,定义好RatingBar,然后在代码里面,就可以通过实现OnRatingBarChangeListener,来获取评分的结果了。

常用属性如下:

1:isIndicator:是否是一个指示器(用户无法进行更改)
2:numStars:显示的星型数量,是一个整数值
3:rating:默认的评分,是浮点类型
4:stepSize:评分的步长,是浮点类型,通常设置成“1.0”
5:style:这个要注意,前面不加“android:”,直接使用style,当然可以不设置,使用默认的风格,如果要设置风格,常用的风格示例如下:
1)style="?android:attr/ratingBarStyleIndicator“
2)style="?android:attr/ratingBarStyleSmall“
3)style="?android:attr/ratingBarStyle"

Gallery

Gallery的使用相对要麻烦一点,首先在布局文件里面要定义

然后在程序里面,需要对Gallery赋值,也就是设置要展示的图片,这就需要自定义一个Adapter,来把图片的数据适配成为Gallery需要的数据,示例如下:

java代码:
  1. class ImageAdapter extends BaseAdapter {  
  2.     private Context mContext;  
  3.     private Integer[] mImageIds = {R.drawable.icon,R.drawable.icon,  
  4.             R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon,  
  5.             R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon};  
  6.     public ImageAdapter(Context c) {    mContext = c;   }  
  7.     public int getCount() { return mImageIds.length;  }  
  8.     public Object getItem(int position) { return position; }  
  9.     public long getItemId(int position) { return position; }  
  10.     public View getView(int position, View convertView, ViewGroup parent) {  
  11.         ImageView imageView = new ImageView(mContext);  
  12.         imageView.setImageResource(mImageIds[position]);  
  13.         imageView.setLayoutParams(new Gallery.LayoutParams(150100));  
  14.         imageView.setScaleType(ImageView.ScaleType.FIT_XY);  
  15.         return imageView;  
  16.     }  
  17. }  
  18.    
然后在程序里面,对Gallery设置Adapter,当然就是刚才实现的自定义的Adapter实例了。
Gallery的事件监听器是:OnItemClickListener,通过它可以实现事件处理

如果要给Gallery设置风格的话,需要在res/values文件夹下面创建一个attrs.xml,示例代码如下:


java代码:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="MyGallery">  
  4.         <attr name="android:galleryItemBackground" />  
  5.     </declare-styleable>  
  6. </resources>  

然后在自定义的适配器里面,在构造方法里面,添加如下的代码:


java代码:
  1. TypedArray attr = mContext.obtainStyledAttributes(R.styleable.MyGallery);  
  2. mGalleryItemBackground = attr.getResourceId(  
  3.       R.styleable.MyGallery_android_galleryItemBackground, 0);  

有了风格过后,在创建ImageView的时候,就可以使用这个风格了,在getView方法里面添加如下代码:


java代码:
  1. imageView.setBackgroundResource(mGalleryItemBackground);  
这样每个ImageView就都会有一个相同的背景了。

AutoComplete

AutoComplete的使用比较简单,首先在布局文件里面配置使用AutoCompleteTextView

然后在程序里面,为它设置值,示例代码如下:


java代码:
  1. AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);  
  2. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.autoc_item, new String[]{"abc","abd","abcd","bcd","bef"});  
  3. textView.setAdapter(adapter);  
由于组件在显示提示数据的时候,是采用的列表的形式,因此需要去为list中的每个项定义一个布局文件,其实非常简单,用TextView即可,比如,在layout文件夹里面添加一个auto_item.xml,里面配置示例如下:

java代码:
  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:padding="10dp"  
  5.     android:textSize="16sp"  
  6.     android:textColor="#ff0000">  
  7. </TextView>  
当然也可使用系统提供的布局,比如:android.R.layout. simple_dropdown_item_1line
 
上面的示例是输入单个值的,要想一次输入多个值,而且每个都想要有自动完成的功能该怎么办呢?可以使用MultiAutoCompleteTextView

首先在布局文件设置使用MultiAutoCompleteTextView,示例代码如下:


java代码:
  1. <MultiAutoCompleteTextView android:id="@+id/autocomplete"  
  2. android:layout_width="fill_parent"  
  3. android:layout_height="wrap_content"  
  4. />  

然后在程序中,为他设置值,示例代码如下:


java代码:
  1. MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.autocomplete);  
  2. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.autoc_item, new String[]{"abc","abd","abcd","bcd","bef"});  
  3. textView.setAdapter(adapter);  
  4. textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());  
最后一句是设置多个值之间用什么分割,不可少。

LinearLayout


java代码:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.   <LinearLayout  
  6.       android:orientation="horizontal"  
  7.       android:layout_width="fill_parent"  
  8.       android:layout_height="fill_parent"  
  9.       android:layout_weight="1">  
  10.       <TextView  
  11.           android:text="red"  
  12.           android:gravity="center_horizontal"  
  13.           android:background="#ff0000"  
  14.           android:layout_width="wrap_content"  
  15.           android:layout_height="fill_parent"  
  16.           android:layout_weight="1"/>  
  17.       <TextView  
  18.           android:text="green"  
  19.           android:gravity="center_horizontal"  
  20.           android:background="#00ff00"  
  21.           android:layout_width="wrap_content"  
  22.           android:layout_height="fill_parent"  
  23.           android:layout_weight="1"/>  
  24.       <TextView  
  25.           android:text="blue"  
  26.           android:gravity="center_horizontal"  
  27.           android:background="#0000ff"  
  28.           android:layout_width="wrap_content"  
  29.           android:layout_height="fill_parent"  
  30.           android:layout_weight="1"/>  

java代码:
  1. <TextView  
  2.           android:text="yellow"  
  3.           android:gravity="center_horizontal"  
  4.           android:background="#aaaa00"  
  5.           android:layout_width="wrap_content"  
  6.           android:layout_height="fill_parent"  
  7.           android:layout_weight="1"/>  
  8.   </LinearLayout>         
  9.   <LinearLayout  
  10.     android:orientation="vertical"  
  11.     android:layout_width="fill_parent"  
  12.     android:layout_height="fill_parent"  
  13.     android:layout_weight="1">  
  14.     <TextView  
  15.         android:text="第一行"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_weight="1"/>  
  19.     <TextView  
  20.         android:text="第二行"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_weight="1"/>  
  24.     <TextView  
  25.         android:text="第三行"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_weight="1"/>  
  29.   </LinearLayout>  
  30. </LinearLayout>  

RelativeLayout


java代码:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent">  
  4.     <TextView  
  5.         android:id="@+id/label"  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content"  
  8.         android:text="请输入:"/>  
  9.     <EditText  
  10.         android:id="@+id/entry"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:background="@android:drawable/editbox_background"  
  14.         android:layout_below="@id/label"/>  
  15.     <Button  
  16.         android:id="@+id/ok"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_below="@id/entry"  
  20.         android:layout_alignParentRight="true"  
  21.         android:layout_marginLeft="10dip"  
  22.         android:text="确定" />  
  23.     <Button  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_toLeftOf="@id/ok"  
  27.         android:layout_alignTop="@id/ok"  
  28.         android:text="取消" />  
  29. </RelativeLayout>  
  30.    

TableLayout


java代码:
  1. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:stretchColumns="1">  
  5.     <TableRow>  
  6.         <TextView android:layout_column="1" android:text="张三" android:padding="3dip" />  
  7.         <TextView  
  8.             android:text="25岁" android:gravity="right" android:padding="3dip" />  
  9.     </TableRow>  
  10.     <TableRow>  
  11.         <TextView android:layout_column="1" android:text="李四" android:padding="3dip" />  
  12.         <TextView  
  13.             android:text="22岁"  
  14.             android:gravity="right"  
  15.             android:padding="3dip" />  
  16.     </TableRow>  
  17.     <View  android:layout_height="2dip" android:background="#FF909090" />  
  18.     <TableRow>  
  19.         <TextView  
  20.             android:text="王五"  
  21.             android:padding="3dip" />  
  22.         <TextView  
  23.             android:text="本科毕业"  
  24.             android:padding="3dip" />  
  25.         <TextView  
  26.             android:text="23岁"  
  27.             android:gravity="right"  
  28.             android:padding="3dip" />  
  29.     </TableRow>  
  30. </TableLayout>  

TableLayout的常用属性如下:

android:stretchColumns :拉伸指定的列来填充一行的空白
android:collapseColumns :隐藏指定的列,隐藏多列用逗号分开,如“1,2”
android:shrinkColumns :收缩指定的列
android:layout_column :widget在一行里面所在的column的索引
android:layout_span :widget跨越的列数,也就是合并的列数

GridView

首先在布局文件里面配置使用GridView,示例代码如下:


java代码:
  1. <GridView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/gridview"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:columnWidth="50dp"  
  6.     android:numColumns="5"  
  7.     android:verticalSpacing="10dp"  
  8.     android:horizontalSpacing="2dp"  
  9.     android:stretchMode="columnWidth"  
  10.     android:gravity="center"  
  11. />  

然后在程序里面,为它设置值,示例代码如下:


java代码:
  1. GridView gridview = (GridView) findViewById(R.id.gridview);  
  2. gridview.setAdapter(new ImageAdapter(this));  

然后在程序里面,为它设置值,需要使用一个自定义的Adapter,示例代码如下:


java代码:
  1. class ImageAdapter extends BaseAdapter {  
  2.     private Context mContext;  
  3.     public ImageAdapter(Context c) { mContext = c;  }  
  4.     public int getCount() {return mThumbIds.length; }  
  5.     public Object getItem(int position) { return null;}  
  6.     public long getItemId(int position) { return 0;   }  
  7.     public View getView(int position, View convertView, ViewGroup parent) {  
  8.         ImageView imageView;  
  9.         if (convertView == null) {  
  10.             imageView = new ImageView(mContext);  
  11.             imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  12.             imageView.setScaleType(ImageView.ScaleType.FIT_XY);  
  13.             imageView.setPadding(00,0,0);  
  14.             imageView.setBackgroundColor(Color.RED);  
  15.         } else {  
  16.             imageView = (ImageView) convertView;  
  17.         }  
  18.         imageView.setImageResource(mThumbIds[position]);  
  19.         return imageView;  
  20.     }  
  21.     private Integer[] mThumbIds = {  
  22.             R.drawable.icon, R.drawable.icon,  
  23.             R.drawable.icon, R.drawable.icon,  
  24.             R.drawable.icon, R.drawable.icon,  
  25.             R.drawable.icon, R.drawable.icon  
  26.     };  
  27. }  

事件处理是实现OnItemClickListener,示例代码如下:


java代码:
  1. gridview.setOnItemClickListener(new OnItemClickListener() {  
  2.         public void onItemClick(AdapterView<?> parent, View v,  
  3.                                             int position, long id) {  
  4.             Toast.makeText(TestWidgets.this"" + position,  
  5.                                           Toast.LENGTH_SHORT).show();  
  6.         }  
  7.     });  

TabLayout

要实现基本的TabLayout,首先需要在布局文件里面定义,示例代码如下:

 

java代码:
  1. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  2.   android:id="@android:id/tabhost"  
  3.   android:layout_width="fill_parent"  
  4.   android:layout_height="fill_parent">  
  5.   <LinearLayout  
  6.     android:orientation="vertical"  
  7.     android:layout_width="fill_parent"  
  8.     android:layout_height="fill_parent"  
  9.     android:padding="5dp">  
  10.     <TabWidget  
  11.       android:id="@android:id/tabs"  
  12.       android:layout_width="fill_parent"  
  13.       android:layout_height="wrap_content" />  
  14.     <FrameLayout  
  15.       android:id="@android:id/tabcontent"  
  16.       android:layout_width="fill_parent"  
  17.       android:layout_height="fill_parent"  
  18.       android:padding="5dp" />  
  19.   </LinearLayout>  
  20. </TabHost>nTabHost就是用来存放多个Tab的容器  

TabWidget就是实现Tab的Widget

FrameLayout是Tab的内容,FrameLayout是Android最简单的布局对象。它被定制为屏幕上的一个空白区域,可以在其中填充一个单一对象 ,所有的子元素将会固定在左上角;不能为FrameLayout中的子元素指定位置。后一个子元素将会直接覆盖前一个子元素。
TabHost必须设置为@android:id/tabhost,TabWidget必须设置android:id为@android:id/tabs,FrameLayout需要设置android:id为@android:id/tabcontent

有了布局过后,就可以直接在程序里面设置Tab了,示例如下:

1:不再继承Activity,而是继承TabActivity,对于Tab布局的实现,现在已经不推荐使用了,建议改用Fragment来实现,但为了学习这个知识,还是沿用。

2:得到TabHost,示例代码如下:


java代码:
  1. TabHost tabHost = getTabHost();  

3:创建一个Intent对象,做为点击tab后的事件响应,示例代码如下:


java代码:
  1. Intent intent = new Intent().setClass(this, MyActivity.class);  
  2. intent.putExtra("showStr""第一个tab");  
  3. Intent设置的Class就是点击tab,内容页显示出来的Activity。  
4:创建一个TabSpec,也就是一个Tab的描述,包含一个tab加上下面的显示内容。简单点说,TabSpec就是一个完整的Tab页。示例代码如下:TabHost.TabSpec spec = tabHost.newTabSpec("FirstTag")
                     .setIndicator("第一个Tab").setContent(intent);

5:然后把这个TabSpec添加到TabHost中即可,示例代码如下:


java代码:
  1. tabHost.addTab(spec);  
6:如法炮制,就可以添加任意多个Tab了。
如果要设置默认选中的Tab,可以使用TabHost的setCurrentTab方法

如果想要给Tab设置图片,方法步骤如下:

1:为每个Tab准备好两张图片,一张是选中时使用,一张是未选中时使用
2:在res/drawable/ 下面,为每个tab增加一个选择器文件,用来告诉tab如何选择使用这些图片,比如my_tab1_selector.xml、 my_tab2_selector.xml,示例代码如下:

java代码:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:drawable="@drawable/icon2"  
  4.           android:state_selected="true" />  
  5.     <item android:drawable="@drawable/icon" />  
  6. </selector>  
3:在程序中,得到Resources对象,可以通过它来获取资源,示例代码如下:

java代码:
  1. Resources res = getResources();  
4:在创建TabSpec的时候,在设置每个TabSpec的Indicator的时候,设置这个Indicator使用的图片资源,示例代码如下:

java代码:
  1. spec = tabHost.newTabSpec("FirstTag").setIndicator(  
  2. "第一个Tab",res.getDrawable(R.drawable.my_tab1_selector))  
  3.                   .setContent(intent);  
5:如法炮制,就可以为每个Tab设置图标了。
 

ListView

对于最基本的ListView,直接在代码里面写,示例如下:


java代码:
  1. public class TestList extends ListActivity {  
  2. public void onCreate(Bundle savedInstanceState) {  
  3. super.onCreate(savedInstanceState);  
  4. // this.setContentView(R.layout.TestList);  
  5. setListAdapter(new ArrayAdapter<String>(this,  
  6.          android.R.layout.simple_list_item_1, new String[]{"111","222","333"}));  
  7. }  
  8. protected void onListItemClick(ListView l,View v,int position,long id){  
  9. super.onListItemClick(l, v, position, id);  
  10. String s = ""+l.getItemAtPosition(position);  
  11. System.out.println("now click="+s);  
  12. }  
  13. }  
  14.    

List的单选模式,示例代码如下:


java代码:
  1. setListAdapter(new ArrayAdapter<String>(this,  
  2.                 android.R.layout. simple_list_item_single_choice, new String[]{"111","222","333}));  
  3. this.getListView().setItemsCanFocus(false);  
  4. this.getListView().setChoiceMode(ListView. CHOICE_MODE_SINGLE);   

List的多选模式,示例代码如下:


java代码:
  1. setListAdapter(new ArrayAdapter<String>(this,  
  2.                 android.R.layout.simple_list_item_multiple_choice, new String[]{"111","222","333}));  
  3. this.getListView().setItemsCanFocus(false);  
  4. this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);  

在ListView里面设置多个值的方法

在ListView的main.xml布局文件中


java代码:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  8.     android:orientation="vertical"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="fill_parent"  
  11.     >  
  12. <ListView  
  13.     android:layout_width="fill_parent"  
  14.     android:layout_height="fill_parent"  
  15.     android:padding="10dp"  
  16.     android:textSize="16sp"  
  17.     android:id="@id/android:list"  
  18.     >  
  19. </ListView>  
  20. </LinearLayout>  
  21. </LinearLayout>  

在ListView的Item的布局文件中


java代码:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <RelativeLayout  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="fill_parent">  
  10. <TextView  
  11.     android:layout_width="wrap_content"  
  12.     android:layout_height="wrap_content"  
  13.     android:id="@+id/userId"  
  14.      
  15.     >  
  16. </TextView>  

java代码:
  1. <TextView  
  2.     android:layout_width="wrap_content"  
  3.     android:layout_height="wrap_content"  
  4.     android:id="@+id/userName"  
  5.     android:layout_marginLeft="20dip"      
  6.     android:layout_toRightOf="@id/userId"  
  7.     >  
  8. </TextView>  
  9. <TextView  
  10.     android:layout_width="wrap_content"  
  11.     android:layout_height="wrap_content"  
  12.     android:id="@+id/userAge"  
  13.     android:layout_marginLeft="20dip"      
  14.     android:layout_toRightOf="@id/userName"  
  15.     >  
  16. </TextView>  
  17. </RelativeLayout>  
  18. </LinearLayout>  
  19.    

在java程序中

1:要继承ListActivity
2:使用Adapter来为List设置值

java代码:
  1. List list = new ArrayList();  
  2.        Map map1 = new HashMap();  
  3.        map1.put("userId","11");  
  4.        map1.put("userName","11N");  
  5.        map1.put("age","11");  
  6.        list.add(map1);         
  7.        Map map2 = new HashMap();  
  8.        map2.put("userId","22");  
  9.        map2.put("userName","22N");  
  10.        map2.put("age","22");  
  11.        list.add(map2);  
  12.                
  13.        setListAdapter(new SimpleAdapter(this, list,R.layout.list,new String[]{"userId","userName","age"}, new int[]{R.id.userId,R.id.userName,R.id.userAge}));      
3:处理item的点击事件

java代码:
  1. protected void onListItemClick(ListView l, View v, int position, long id) {  
  2. super.onListItemClick(l, v, position, id);  
  3. System.out.println("sssssssss===="+l.getItemAtPosition(position));  

菜单

在Android系统中,菜单分成三种

1:Options Menu :就是当你按MENU键的时候显示的菜单
2:Context Menu :在点击的地方浮动出现的菜单
3:Submenu  : 子菜单

创建菜单资源

通常在菜单资源的xml里面,把菜单项都预先定义好。虽然也可以在程序中通过代码添加菜单项,但更推荐使用菜单资源的xml。
1:在res/menu/ 下创建菜单的xml文件,示例代码如下:

java代码:
  1. <menu xmlns:android="http://schemas.android.com/apk/res/android">  
  2.     <item android:id="@+id/to_add"  
  3.           android:icon="@drawable/icon"  
  4.           android:title="转向新增" />  
  5.     <item android:id="@+id/to_update"  
  6.           android:icon="@drawable/icon"  
  7.           android:title="转向修改" />  
  8. </menu>  
2:在代码里面使用菜单资源文件,示例如下:

java代码:
  1. public boolean onCreateOptionsMenu(Menu menu) {     
  2. MenuInflater inflater = getMenuInflater();     
  3. inflater.inflate(R.menu.test_menu, menu);     
  4. return true;  
  5. }  

响应菜单,在程序里面,需要写代码来实现菜单被点击后要实现的功能


java代码:
  1. public boolean onOptionsItemSelected(MenuItem item) {  
  2. switch (item.getItemId()) {  
  3. case R.id.to_add:  
  4. Log.i("javass","now to add"); break;  
  5. case R.id.to_update:  
  6. Log.i("javass","now to update"); break;  
  7. defaultreturn super.onOptionsItemSelected(item);  
  8. }  
  9. return true;  
  10. }  
Option Menu一般最多显示六个,如果更多的话,会出现一个更多的按钮。
Android3.0以后,可以直接在菜单描述文件里面配置android:onClick,指定当菜单被点击时要调用的方法,该方法接受一个MenuItem参数。示例如下:

java代码:
  1. public void myToAdd(MenuItem item) {  
  2. Log.i("javass","now in myToAdd");  
  3. }  
当配置android:onClick后,上面的onOptionsItemSelected里面对应的菜单处理就不会被调用了

上下文菜单

上下文菜单就相当于PC上的右击,创建上下文菜单:

1:菜单项同样来源于菜单资源文件
2:为需要上下文菜单的view进行注册,示例如下:

java代码:
  1. this.registerForContextMenu(btnRun);  
3:覆写onCreateContextMenu方法,示例如下:

java代码:
  1. public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo info) {   
  2. super.onCreateContextMenu(menu, v, info);   
  3. MenuInflater inflater = getMenuInflater();   
  4. inflater.inflate(R.menu.test_menu, menu);  
  5. }  
4:处理菜单事件,示例如下:

java代码:
  1. public boolean onContextItemSelected(MenuItem item) {  
  2. AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();  
  3. switch (item.getItemId()) {  
  4. case R.id. new_file:  
  5. Log.i("onContextItemSelected""now ruuuuuuuuuuu");  
  6. return true;  
  7. defaultreturn super.onContextItemSelected(item);  
  8. }  
  9. }  

子菜单

  1. <group android:id="@+id/group1">       
  2.     <item android:id="@+id/create_new"  
  3.         android:icon="@drawable/ic_launcher"                   
  4.         android:title="now new" />             
  5.     <item android:id="@+id/open"             
  6.         android:icon="@drawable/ic_launcher"       
  7.         android:title="now open" />         
  8. </group>  

分组后能干什么

1:可以通过使用setGroupVisible()方法来显示或隐藏一组菜单
2:可以通过使用setGroupEnabled()方法来使一组菜单可用还是不可用
3:可以通过使用setGroupCheckable()方法来使一组菜单可选还是不可选

可选菜单

可选菜单非常简单,只需要在菜单资源文件中配置即可,示例如下:

1:对于单个菜单项,添加android:checkable,即可变成带复选框的菜单
2:对于分组的菜单,直接在Group上添加android:checkableBehavior的配置,它有三个选项:单选、复选、不能选择。

可选菜单一般针对上下文菜单和子菜单

要注意:使用可选菜单的时候,选择的状态必须要通过程序来保存,否则下次进入的时候,是不会保留你选择的状态的。可以在对应的事件处理里面保留选项的状态,然后在菜单重新绘制的时候,重新设置状态。

1:对于Options的子菜单,可以在onPrepareOptionsMenu方法里面设置状态,示例代码如下:


java代码:
  1. public boolean onPrepareOptionsMenu(Menu menu) {  
  2. super.onPrepareOptionsMenu(menu);  
  3. MenuItem mi = menu.findItem(R.id.create_new);  
  4. mi.setChecked(flag);  
  5. return true;  
  6. }  
2:对于上下文菜单或其子菜单,可以直接在onCreateContextMenu方法里面设置,因为这个方法每次都会重新执行。

动态修改菜单项

有些时候,需要动态的修改菜单项,该怎么实现呢?

对于Options的菜单及其子菜单

1:覆盖实现onPrepareOptionsMenu方法。
2:在这个方法里面使用menu的方法来维护菜单项
 

onPrepareOptionsMenu方法和onCreateOptionsMenu方法

1:onCreateOptionsMenu只是在第一次创建菜单的时候调用一次,以后就不调用了
2:onPrepareOptionsMenu方法是每次显示菜单的时候,都要调用一次的方法。
对于上下文的菜单及其子菜单,可以直接在onCreateContextMenu方法里面设置,因为这个方法每次都会重新执行。
 

编程式实现菜单

前面是声明式实现菜单,也可以采用编程式的方式来实现菜单,常用API为:

1:Menu
2:MenuItem
3:SubMenu,与MenuItem平行
 

关于Menu的图标

1:子菜单的项不能显示图标
2:上下文菜单和项都不能显示图标
3:带图标的菜单项不能加上复选框
 

Action Bar基本概念

是什么

ActionBar是用来替换传统的title bar的一种新的Widget,通常显示在屏幕的顶端。
通常情况下,Acviton Bar带着应用的Logo放置到最左端,紧接着是应用的title,而其他可选项放置在右边。
在Android3.0以后,所有的Activity都缺省的带着ActionBar了。

特点

1:显示Option Menu中的项,让用户能直接操作
2:提供tab来在多个Fragment之间导航
3:提供drop-down的导航列表
4:提供直接交户的Action Views,比如搜索条等。

使用Action Bar

删除Action Bar

1:Android系统缺省用的是“holographic ” theme,是有Action Bar的,可以在manifext配置文件中配置Activity的时候,指定不需要Action Bar,示例如下:
2:也可以在程序中设置,示例如下:

java代码:
查看复制到剪贴板打印
  1. ActionBar actionBar = getActionBar();  
  2. actionBar.hide();  
添加Action Items
一个Action Item就相当于Option Menu中的一个选项。要想把菜单项显示成为Action Item,只需要在菜单项的配置文件中,设置android:showAsAction=“ifRoom” 。也可使用程序设置:
menu.getItem(0).setShowAsAction(MenuItem. SHOW_AS_ACTION_IF_ROOM);
Action Items的事件响应,就是Option Menu的事件响应
  1. ActionBar actionBar = getActionBar();  
  2. actionBar.setDisplayUseLogoEnabled(true);  
  3. actionBar.setLogo(R.drawable.app_sample_code);  
  4. actionBar.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.app_sample_code));  
  5.    
2:可以通过修改theme的方式,比如:

java代码:
  1. <activity android:name=".ExampleActivity"           
  2. android:theme="@android:style/Theme.Holo.Light" />  

Dialogs

什么是Dialog

就是在当前Activity上弹出的小窗口,也就是对话框。Dialog通常是附属于Activity的。

常见的Dialog

1:AlertDialogA dialog :就是弹出提示或警告信息的对话框
2:ProgressDialog : 进度条
3:DatePickerDialog :选择日期的对话框
4:TimePickerDialog :选择时间的对话框

创建和显示Dialog

一般会在onCreateDialog() 方法里面去创建Dialog,如果要在这个方法外创建Dialog,那么需要调用setOwnerActivity(Activity)方法来把Dialog和Activity关联起来。
想要显示Dialog的时候,调用showDialog(int)方法即可。

关闭Dialog

可以在Dialog对象里面使用dismiss()方法,也可以在Activity里面使用dismissDialog(int)方法。
 

Dismiss listener

如果应用程序想要处理dissmiss dialog的事件,可以定义一个类来实现DialogInterface.OnDismissListener,里面有一个setOnDismissListener()的方法,会在Dialog被销毁的时候触发。
当然,Listener需要被注册,可以使用setOnCancelListener()方法。

实现Dialog风格的Activity

在 AndroidManifest.xml的对应的Activity设置上添加:
android:theme="@android:style/Theme.Dialog"

Alert Dialog

  1. final CharSequence[] items = { "红色""绿色""蓝色" };  
  2. AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  3. builder.setTitle("请选择颜色");  
  4. builder.setItems(items, new DialogInterface.OnClickListener() {  
  5. public void onClick(DialogInterface dialog, int item) {  
  6. Toast.makeText(getApplicationContext(), items[item],  
  7. Toast.LENGTH_SHORT).show();  
  8. }  
  9. });  
  10. AlertDialog alert = builder.create();  
如果想要是单选的列表项,那就不要使用setItems,而用setSingleChoiceItems(),示例如下:

java代码:
  1. final CharSequence[] items = { "红色""绿色""蓝色" };  
  2. AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  3. builder.setTitle("请选择颜色");  
  4. builder.setSingleChoiceItems(items, -1,new DialogInterface.OnClickListener() {  
  5. public void onClick(DialogInterface dialog, int item) {  
  6. Toast.makeText(getApplicationContext(), items[item],  
  7. Toast.LENGTH_SHORT).show();  
  8. dialog.dismiss();  
  9. }  
  10. });  
  11. AlertDialog alert = builder.create();  
如果想要是多选的列表项,那就不要使用setItems,而用setMultiChoiceItems (),示例如下:
  1. HandlerThread ht = new HandlerThread("MyThread");  
  2. Handler h = null;  
2:在启动Dialog的方法里面:

java代码:
  1. if(!ht.isAlive()){  
  2.     ht.start();  
  3.     h = new Handler(ht.getLooper()){  
  4. public void handleMessage(Message msg) {  
  5. try {  
  6. Thread.sleep(5000L);  
  7. catch (InterruptedException e) {  
  8. e.printStackTrace();  
  9. }  
  10. HelloWorldActivity.this.dismissDialog(1);  
  11. super.handleMessage(msg);  
  12. }     
  13.     };  
  14. }  
  15. Message msg = h.obtainMessage();  
  16. msg.sendToTarget();  

使用Progress Bar,同样需要配合Handler来执行,示例如下:

1:创建Progress Bar,在onCreateDialog方法里面,示例如下:


java代码:
  1. dialog = new ProgressDialog(HelloWorldActivity.this);  
  2. dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  3. dialog.setMessage("装载中...");  
  4. dialog.setCancelable(false);  
  5. dialog.setMax(10);  

2:定义Handler和Runnable


java代码:
  1. Handler h = new Handler() {  
  2. public void handleMessage(Message msg) {  
  3. super.handleMessage(msg);  
  4. if(msg.arg1 < dialog.getMax()){dialog.setProgress(msg.arg1);}  
  5. else{dialog.dismiss();}}};  
  6. int num = 0;  
  7. Runnable r = new Runnable() {  
  8. public void run() {  
  9. Message msg = h.obtainMessage();  
  10. msg.arg1 = num++;  
  11. h.sendMessage(msg);  
  12. h.postDelayed(r, 1000L);  
  13. }};  
3:在显示dialog的同时,要启动Handler的运行,也就是 h.post(r);
  1. pb = (ProgressBar)HelloWorldActivity.this.findViewById(R.id.progress_bar);  
  2. pb.setMax(10);  
  3. pb.setVisibility(View.VISIBLE);  
  4. h.post(r);  
3: Handler和Runnable的实现跟前面类似,这里就不去写了。

Customer Dialog

如果想要使用自己订制的Dialog,那么首先需要定义自己的布局,示例如下:


java代码:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layout_root"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="horizontal"  
  7.     android:padding="10dp" >  
  8.     <ImageView  
  9.         android:id="@+id/image"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="fill_parent"  
  12.         android:layout_marginRight="10dp" />  
  13.     <TextView  
  14.         android:id="@+id/text"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="fill_parent"  
  17. />  
  18. </LinearLayout>  
  1. Handler h = new Handler() {  
  2. public void handleMessage(Message msg) {  
  3. super.handleMessage(msg);  
  4. //使用what来区分是什么样的消息  
  5. switch (msg.what){  
  6. case 1 :  
  7. //这里进行消息的处理  
  8. break;  
  9. default :  
  10. //这里进行消息的处理  
  11. }  
  12. }};  
  13. int what = 0;  
  14. Runnable r = new Runnable() {  
  15. public void run() {  
  16. Message msg = h.obtainMessage();  
  17. msg.obj = "1234567";//传递的参数  
  18. msg.what = what;  
  19. what++;  
  20. h.sendMessage(msg);  
  21. h.postDelayed(r, 1000L);  
  22. }};  

如果想要启动新的线程来处理,使用HandlerThread的示例如下:

1:先定义Handler和HandlerThread:

java代码:
  1. HandlerThread ht = new HandlerThread("MyThread");  
  2. Handler h = null;  
2:在某个地方(比如某个按钮操作里面)启动HandlerThread:

java代码:
  1. ht.start();  
  2. h = new Handler(ht.getLooper()){  
  3. public void handleMessage(Message msg) {  
  4. super.handleMessage(msg);  
  5. //这里真正处理消息  
  6. Toast.makeText(getApplicationContext(),"msg="+msg.obj,Toast.LENGTH_LONG).show();  
  7. }     
  8. };  
3:在某个地方(比如某个按钮操作里面)发送消息:

java代码:
  1. Message msg = h.obtainMessage();  
  2. msg.obj="test message";  
  3. msg.sendToTarget();  
4:在某个地方(比如某个按钮操作里面)可以停止HandlerThread,使用stop方法
 

Notifications

Android系统中,有如下通知类型:

1:Toast Notification:展示后台的简短消息
2:Status Bar Notification:展示后台对用户的提示,通常需要用户响应
3:Dialog Notification:Activity相关的通知

指定Toast的位置,使用Gravity属性,示例如下:


java代码:
  1. Toast t = Toast.makeText(HelloWorldActivity.this,"okok", Toast.LENGTH_SHORT);  
  2. t.setGravity(Gravity.TOP|Gravity.LEFT, 5,10);  
  3. t.show();  

订制Toast,需订制布局文件,这里使用上一个示例的布局文件来示例,程序写法如下:


java代码:
  1. LayoutInflater inflater = getLayoutInflater();  
  2. View layout = inflater.inflate(R.layout.my_dialog,  
  3. (ViewGroup) findViewById(R.id.layout_root));  
  4. ImageView image = (ImageView) layout.findViewById(R.id.image);  
  5. image.setImageResource(R.drawable.ic_launcher);  
  6. TextView text = (TextView) layout.findViewById(R.id.text);  
  7. text.setText("欢迎您的到来!");  
  8. Toast t = new Toast(HelloWorldActivity.this);  
  9. t.setView(layout);  
  10. t.setGravity(Gravity.CENTER|Gravity.LEFT, 5,10);  
  11. t.show();  
  12.    

Status Bar Notification :可以由Activity或Service发出。示例如下:


java代码:
NotificationManager 是一个系统级的服务
可以通过NotificationManager 来取消自己的Status Bar Notification,就是使用notity时为通知设置的一个ID,示例如下:
mNotificationManager.cancel(1);//1就是前面notity时传递的id值,必须唯一
为Notification添加提示声音
1:使用系统默认的声音:
notification.defaults |= Notification. DEFAULT_SOUND;
2:使用自己上传的声音:
notification.sound=Uri. parse("file:///data/data/cn.javass/test_cbr.mp3");
为Notification添加提示振动
1:首先要设置权限:<uses-permission android:name= "android.permission.VIBRATE" />
2:使用系统缺省的振动:notification.defaults |= Notification.DEFAULT_VIBRATE;
3:自己定义振动的参数:
long[] vibrate = {0,100,200,300};
otification.vibrate = vibrate;
为Notification添加 闪烁 提示,也就是LED灯的闪烁
1:使用系统缺省的:notification.defaults |= Notification.DEFAULT_LIGHTS;
2:自己定义闪烁的参数:
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification. FLAG_SHOW_LIGHTS;
 

为Notification添加其他特性
1:FLAG_AUTO_CANCEL:当用户点击这个通知后,通知自动消失
2:FLAG_INSISTENT:重复播放音乐直到用户响应
3:FLAG_ONGOING_EVENT:在通知窗口增加Ongoing的标记
4:FLAG_NO_CLEAR:标识通知不能被清除掉

客户化的Notification,只是客户化在通知信息列表里面的展示,其他的跟前面仍然是一样的,示例如下:
1:自己订制一个Layout的配置文件,这里使用前面示例用的Layout
2:不再调用notification.setLatestEventInfo这句话,修改成:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.my_dialog);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, "重要通知");//这个前面的layout里面没有,可以注掉,也可以自己去layout里面添加上
contentView.setTextViewText(R.id.text, "测试");
 
//设置在通知信息列表里面显示的视图
otification.contentView = contentView;
//设置点击通知信息时触发的事件
otification.contentIntent = contentIntent;
 
视频配套PPT,视频地址【 

Android4开发入门经典独家视频课程
 原创内容 转自请注明【 http://sishuok.com/forum/blogPost/list/0/2622.html#7471