[功能]

PopupWindow作为一种用户提醒 而且其开销也比Activity要小

[代码 步骤]

1. 定义布局 供PopupWindow使用 如:hello.xml

Java代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. android:padding="10dp"
  7. >
  8. <ImageView
  9. android:id="@+id/image"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:src="@drawable/robot"/>
  13. <LinearLayout
  14. android:orientation="vertical"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:paddingLeft="20dip"
  18. >
  19. <TextView
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="HelloPop!"
  23. />
  24. <Button
  25. android:id="@+id/helloButton"
  26. android:layout_width="100dip"
  27. android:layout_height="wrap_content"
  28. android:text="OK"
  29. />
  30. </LinearLayout>
  31. </LinearLayout>

2. 通过LayoutInflater 得到hello.xml 的 View view

Java代码
  1. view=this.getLayoutInflater().inflate(R.layout.hello,null);

3. 创建PopupWindow pop 使用上面布局文件view

Java代码
  1. pop=newPopupWindow(view,500,200);

4. 弹出PopupWindow

* 定义布局文件:main.xml 包括一个Button

Java代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:id="@+id/main"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="popdemo!"
  12. />
  13. <Button
  14. android:id="@+id/button"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:text="topop!"
  18. />
  19. </LinearLayout>

* 弹出:有2种方式:一个是下拉方式 一个是指定位置

- 下拉:

Java代码
  1. findViewById(R.id.button).setOnClickListener(newView.OnClickListener(){
  2. publicvoidonClick(Viewv){
  3. //TODOAuto-generatedmethodstub
  4. pop.showAsDropDown(v);
  5. }
  6. });

- 指定位置:

Java代码
  1. findViewById(R.id.button).setOnClickListener(newView.OnClickListener(){
  2. publicvoidonClick(Viewv){
  3. //TODOAuto-generatedmethodstub
  4. pop.showAtLocation(findViewById(R.id.main),Gravity.CENTER,20,20);
  5. }
  6. });

5. 取消

Java代码
  1. view.findViewById(R.id.helloButton).setOnClickListener(newView.OnClickListener(){
  2. publicvoidonClick(Viewv){
  3. //TODOAuto-generatedmethodstub
  4. pop.dismiss();
  5. }
  6. });

6. 其他问题:

* 发现很多人对PopupWindow 里面包含ListView后 对具体哪个item被点击的获取有疑问 所以就顺便测试一下 发现和普通用法一样啊 没什么特别之处啊 现在把用法和大家分享分享

写道 因为ListView是展开显示的 会导致不美观 所以以Spinner为例

6.1. 定义包含Spinner 的布局文件 hello.xml

Java代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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
  8. android:orientation="horizontal"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. >
  12. <ImageView
  13. android:id="@+id/image"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:src="@drawable/robot"/>
  17. <TextView
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="HelloPop!"
  21. />
  22. </LinearLayout>
  23. <Spinner
  24. android:id="@+id/spinner"
  25. android:layout_width="wrap_content"
  26. android:layout_height="40dip"/>
  27. </LinearLayout>

6.2. 得到Spinner的实例:spinner

Java代码
  1. spinner=(Spinner)view.findViewById(R.id.spinner);

6.3. 绑定Spinner与具体数据 本例以联系人为例

Java代码
  1. publicvoidspecifySpinner(){
  2. Cursorc=getContentResolver().query(People.CONTENT_URI,
  3. null,null,null,null);
  4. SimpleCursorAdapteradapter=newSimpleCursorAdapter(this,
  5. android.R.layout.simple_list_item_1,c,
  6. newString[]{People.NAME},
  7. newint[]{android.R.id.text1});
  8. adapter.setDropDownViewResource(
  9. android.R.layout.simple_spinner_dropdown_item);
  10. spinner.setAdapter(adapter);
  11. }

写道 别忘了联系人访问权限:

<uses-permission android:name=" android.permission.READ_CONTACTS" />

6.4. 具体item的获取:

Java代码
  1. spinner.setOnItemSelectedListener(newOnItemSelectedListener(){
  2. publicvoidonItemSelected(AdapterView<?>adapter,Viewv,
  3. intpos,longid){
  4. updateTitle(pos);
  5. }
  6. publicvoidonNothingSelected(AdapterView<?>arg0){
  7. //TODOAuto-generatedmethodstub
  8. }
  9. });

写道 updateTitle(int) 用来把位置在标题中显示

public void updateTitle(int i){
this.setTitle("HelloPop:"+i);
}

6.5. emulator 运行截图:

原文:http://www.javaeye.com/topic/604462

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. android标题栏中添加返回按钮
  3. ubuntu下android源代码以及内核的获取
  4. 转载:Android(安卓)常用代码集合
  5. Android(安卓)Toast提示封装实例代码
  6. 方法一、使用Handler和Thread(线程)实现定时器
  7. 坑爹的minSdkVersion配置
  8. Android(安卓)四种绑定监听事件的方式
  9. Android:UI控件AutoCompleteTextView、MultiAutoCompleteTextView

随机推荐

  1. Rockie's Android(安卓)Porting Guide(4)—
  2. Android(安卓)assets文件夹之位置放置和
  3. Android——多语言适配
  4. Ubuntu 10.10 编译Android(安卓)2.2
  5. Android(安卓)SnackBar
  6. Android使用腾讯X5内核替换原生webview
  7. android项目中每个文件的作用
  8. android和html交互--动态注入方法
  9. Android(安卓)程序员指南 PDF下载
  10. android中usb数据通信速率慢问题解决办法