0
点赞
收藏
分享

微信扫一扫

Android中PopupWindow的使用


[java]  view plain  copy


 print

?



  1. public class PopUpActivity extends Activity {  
  2. /** Called when the activity is first created. */  
  3. @Override  
  4. public void onCreate(Bundle savedInstanceState) {  
  5. super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7. this);  
  8. // 引入窗口配置文件  
  9. null);  
  10. // 创建PopupWindow对象  
  11. final PopupWindow pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);  
  12.         Button btn = (Button) findViewById(R.id.btn);  
  13. // 需要设置一下此参数,点击外边可消失  
  14. new BitmapDrawable());  
  15. //设置点击窗口外边窗口消失  
  16. true);  
  17. // 设置此参数获得焦点,否则无法点击  
  18. true);  
  19. new OnClickListener() {  
  20.               
  21. @Override  
  22. public void onClick(View v) {  
  23. if(pop.isShowing()) {  
  24. // 隐藏窗口,如果设置了点击窗口外小时即不需要此方式隐藏  
  25.                     pop.dismiss();  
  26. else {  
  27. // 显示窗口  
  28.                     pop.showAsDropDown(v);  
  29.                 }  
  30.                   
  31.             }  
  32.         });  
  33.     }  
  34. }  


main.xml


[java]  view plain  copy


 print

?



  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. "fill_parent"  
  4. "fill_parent"  
  5. "vertical" >  
  6.   
  7.     <Button  
  8. "@+id/btn"  
  9. "wrap_content"  
  10. "wrap_content"  
  11. "dianji" />  
  12.   
  13. </LinearLayout>  



main2.xml


[java]  view plain  copy


 print

?



  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. "fill_parent"  
  4. "fill_parent"  
  5. "vertical" >  
  6.   
  7.     <Button  
  8. "@+id/a"  
  9. "wrap_content"  
  10. "wrap_content"  
  11. "AAAAA" />  
  12.   
  13.     <Button  
  14. "wrap_content"  
  15. "wrap_content"  
  16. "BBBBB" />  
  17.   
  18.     <Button  
  19. "wrap_content"  
  20. "wrap_content"  
  21. "CCCCC" />  
  22.   
  23.     <Button  
  24. "wrap_content"  
  25. "wrap_content"  
  26. "DDDDD" />  
  27.   
  28. </LinearLayout>  

举报

相关推荐

0 条评论