PopupWindow 使用及延伸

PopupWindow

 

[功能]

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

 

 

[代码 步骤]

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

 

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.     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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    >
<ImageView  
	android:id="@+id/image"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/robot" />
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dip"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="HelloPop!"
    />
<Button  
	android:id="@+id/helloButton"
    android:layout_width="100dip" 
    android:layout_height="wrap_content" 
    android:text="OK"
    />
 </LinearLayout>
</LinearLayout>

 

 

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

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

 

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

Java代码   收藏代码
  1. pop =  new  PopupWindow(view, 500 , 200 );  
pop = new PopupWindow(view,500,200);

 

4. 弹出PopupWindow

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

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: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="pop demo!"   
  12.     />  
  13. <Button    
  14.     android:id="@+id/button"   
  15.     android:layout_width="fill_parent"    
  16.     android:layout_height="wrap_content"    
  17.     android:text="to pop!"   
  18.     />  
  19. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="pop demo!"
    />
<Button  
	android:id="@+id/button"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="to pop!"
    />
</LinearLayout>

 

 

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

- 下拉:

Java代码   收藏代码
  1. findViewById(R.id.button).setOnClickListener( new  View.OnClickListener() {  
  2.               
  3.             public   void  onClick(View v) {  
  4.                     // TODO Auto-generated method stub   
  5.                 pop.showAsDropDown(v);  
  6.             }  
  7.     
  8.     });  
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                    // TODO Auto-generated method stub
            	pop.showAsDropDown(v);
            }
  
    });

 

 

- 指定位置:

Java代码   收藏代码
  1. findViewById(R.id.button).setOnClickListener( new  View.OnClickListener() {  
  2.               
  3.             public   void  onClick(View v) {  
  4.                     // TODO Auto-generated method stub   
  5.                 pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 2020 );  
  6.                   
  7.             }  
  8.     
  9.     });  
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                    // TODO Auto-generated method stub
            	pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 20, 20);
            	
            }
  
    });

 

 

5. 取消

Java代码   收藏代码
  1. view.findViewById(R.id.helloButton).setOnClickListener( new  View.OnClickListener() {  
  2.               
  3.             public   void  onClick(View v) {  
  4.                     // TODO Auto-generated method stub   
  5.                 pop.dismiss();  
  6.                   
  7.             }  
  8.     
  9.     });  
view.findViewById(R.id.helloButton).setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                    // TODO Auto-generated method stub
            	pop.dismiss();
            	
            }
  
    });

 

6. 其他问题:

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

 

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

 

 

6.1. 定义包含Spinner 的布局文件 hello.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   
  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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<ImageView  
	android:id="@+id/image"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/robot" />
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="HelloPop!"
    />
</LinearLayout>
<Spinner 
        android:id="@+id/spinner" 
        android:layout_width="wrap_content" 
        android:layout_height="40dip"/>
</LinearLayout>

 

6.2. 得到Spinner的实例:spinner

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

 

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

Java代码   收藏代码
  1. public   void  specifySpinner(){  
  2.         Cursor c = getContentResolver().query(People.CONTENT_URI,   
  3.                 nullnullnullnull );  
  4.         SimpleCursorAdapter adapter = new  SimpleCursorAdapter( this ,  
  5.                 android.R.layout.simple_list_item_1,c,   
  6.                 new  String[] {People.NAME},   
  7.                 new   int [] {android.R.id.text1});  
  8.         adapter.setDropDownViewResource(  
  9.                 android.R.layout.simple_spinner_dropdown_item);  
  10.           
  11.           
  12.         spinner.setAdapter(adapter);  
  13.     }  
public void specifySpinner(){
    	Cursor c = getContentResolver().query(People.CONTENT_URI, 
                null, null, null, null);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,c, 
                new String[] {People.NAME}, 
                new int[] {android.R.id.text1});
        adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        
        
        spinner.setAdapter(adapter);
    }

 

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

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

 

6.4. 具体item的获取:

Java代码   收藏代码
  1. spinner.setOnItemSelectedListener( new  OnItemSelectedListener(){  
  2.   
  3.             public   void  onItemSelected(AdapterView<?> adapter,View v,  
  4.                     int  pos,  long  id) {  
  5.                 updateTitle(pos);  
  6.             }  
  7.   
  8.             public   void  onNothingSelected(AdapterView<?> arg0) {  
  9.                 // TODO Auto-generated method stub   
  10.                   
  11.             }  
  12.   
  13.         });  
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

            public void onItemSelected(AdapterView<?> adapter,View v,
                    int pos, long id) {
            	updateTitle(pos);
            }

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

        });

 

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

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

 

 

6.5. emulator 运行截图:

 

 http://www.iteye.com/topic/604462

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PopupWindow是一种可以在当前界面上方显示的弹出窗口,通常用于显示一些额外的信息或者提供用户操作的选项。在Android中,可以使用PopupWindow类来创建弹出窗口。 以下是使用PopupWindow的一般步骤: 1. 创建PopupWindow对象:使用PopupWindow的构造函数创建一个PopupWindow对象。 2. 设置PopupWindow的属性:设置PopupWindow的大小、位置、背景等属性。 3. 设置PopupWindow的内容视图:使用setContentView方法设置PopupWindow的内容视图,这可以是一个布局文件或者一个View对象。 4. 显示PopupWindow使用showAsDropDown、showAtLocation等方法显示PopupWindow。 5. 处理PopupWindow的事件:设置PopupWindow的监听器,处理PopupWindow的各种事件。 以下是一个简单的例子,展示如何使用PopupWindow: ``` // 创建PopupWindow对象 PopupWindow popupWindow = new PopupWindow(context); // 设置PopupWindow的属性 popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setFocusable(true); // 设置PopupWindow的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 显示PopupWindow popupWindow.showAsDropDown(anchorView); // 处理PopupWindow的事件 contentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 popupWindow.dismiss(); } }); ``` 在上面的代码中,我们创建了一个PopupWindow对象,并设置了宽高、背景等属性。然后,我们使用LayoutInflater加载了一个布局文件作为PopupWindow的内容视图,并使用setContentView方法设置了PopupWindow的内容视图。最后,我们使用showAsDropDown方法显示了PopupWindow,并设置了一个点击事件处理器来处理用户点击PopupWindow的事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值