Android中 Popwindow的使用

1.使用 popwindow实现如下效果:

2.代码如下:MainActivity.java

 

  1. package mtpop.window.main;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.res.Resources;  
  6. import android.os.Bundle;  
  7. import android.view.Gravity;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.view.ViewGroup.LayoutParams;  
  12. import android.widget.Button;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.PopupWindow;  
  15. import android.widget.PopupWindow.OnDismissListener;  
  16. import android.widget.TextView;  
  17.   
  18. public class MainActivity extends Activity  
  19. {  
  20.   
  21.     private static final String TAG = "MainActivity";  
  22.     private Button button;  
  23.   
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState)  
  26.     {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.   
  30.         button = (Button) findViewById(R.id.button);  
  31.         button.setOnClickListener(new OnClickListener()  
  32.         {  
  33.             @Override  
  34.             public void onClick(View v)  
  35.             {  
  36.                 // 显示 popupWindow   
  37.                 PopupWindow popupWindow = makePopupWindow(MainActivity.this);  
  38.                 int[] xy = new int[2];  
  39.                 button.getLocationOnScreen(xy);  
  40.                 popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,-xy[0]/2,xy[1]+button.getWidth());  
  41.                 //popupWindow.showAsDropDown(button,0, 0);   
  42.             }  
  43.         });  
  44.     }  
  45.   
  46.     // 创建一个包含自定义view的PopupWindow   
  47.     private PopupWindow makePopupWindow(Context cx)  
  48.     {  
  49.         PopupWindow window;  
  50.         window = new PopupWindow(cx);  
  51.           
  52.         //View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null);   
  53.         //window.setContentView(contentView);   
  54.         Button b1 = new Button(this);  
  55.         b1.setText("first");  
  56.         b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));  
  57.           
  58.         Button b2 = new Button(this);  
  59.         b2.setText("Second");  
  60.         b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));  
  61.           
  62.         LinearLayout linearLayout = new LinearLayout(this);  
  63.         linearLayout.addView(b1);  
  64.         linearLayout.addView(b2);  
  65.         linearLayout.setOrientation(LinearLayout.VERTICAL);  
  66.           
  67.         window.setContentView(linearLayout);  
  68.         window.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_bg));  
  69.         window.setWidth(DisplayManager.dipToPixel(150));  
  70.         window.setHeight(DisplayManager.dipToPixel(150));  
  71.           
  72.         // 设置PopupWindow外部区域是否可触摸   
  73.         window.setFocusable(true); //设置PopupWindow可获得焦点   
  74.         window.setTouchable(true); //设置PopupWindow可触摸   
  75.         window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸   
  76.         return window;  
  77.     }  
  78. }  
package mtpop.window.main;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

public class MainActivity extends Activity
{

	private static final String TAG = "MainActivity";
	private Button button;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// 显示 popupWindow
				PopupWindow popupWindow = makePopupWindow(MainActivity.this);
				int[] xy = new int[2];
				button.getLocationOnScreen(xy);
				popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,-xy[0]/2,xy[1]+button.getWidth());
				//popupWindow.showAsDropDown(button,0, 0);
			}
		});
	}

	// 创建一个包含自定义view的PopupWindow
	private PopupWindow makePopupWindow(Context cx)
	{
		PopupWindow window;
		window = new PopupWindow(cx);
		
		//View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null);
		//window.setContentView(contentView);
		Button b1 = new Button(this);
		b1.setText("first");
		b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		
		Button b2 = new Button(this);
		b2.setText("Second");
		b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		
		LinearLayout linearLayout = new LinearLayout(this);
		linearLayout.addView(b1);
		linearLayout.addView(b2);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		
		window.setContentView(linearLayout);
		window.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_bg));
		window.setWidth(DisplayManager.dipToPixel(150));
		window.setHeight(DisplayManager.dipToPixel(150));
		
		// 设置PopupWindow外部区域是否可触摸
		window.setFocusable(true); //设置PopupWindow可获得焦点
		window.setTouchable(true); //设置PopupWindow可触摸
		window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
		return window;
	}
}

3. main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="@android:color/darker_gray"  
  11.         android:orientation="horizontal" >  
  12.   
  13.         <TextView  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_weight="1"  
  17.             android:text="Title" />  
  18.   
  19.         <Button  
  20.             android:id="@+id/button"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="click" />  
  24.     </LinearLayout>  
  25.   
  26. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Title" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="click" />
    </LinearLayout>

</LinearLayout>

注意:    

     * 新建一个popupWindow弹出框 popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待,    

     * 这和AlertDialog不同哦,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。     *

 

******************************************************************

1.PopupWindow的隐藏

  

  1. final PopupWindow window = mPageStatWin;  
  2. if(null != window && window.isShowing()) {  
  3.     win.dismiss();  
  4. }  
final PopupWindow window = mPageStatWin;
if(null != window && window.isShowing()) {
    win.dismiss();
}

2.Popupwindow的显示及位置设置

window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10);


第一个参数指定PopupWindow的锚点view,即依附在哪个view上。

第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。


   

//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
window. showAsDropDown (anchor ) ;
//xoff,yoff基于anchor的左下角进行偏移。
window. showAsDropDown (anchor, xoff, yoff ) ;
如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。


         * popupWindow.showAsDropDown(View view)弹出对话框,位置在紧挨着view组件    

         * showAsDropDown(View anchor, int xoff, int yoff)弹出对话框,位置在紧挨着view组件,x y 代表着偏移量     

         * showAtLocation(View parent, int gravity, int x, int y)弹出对话框 

          * parent 父布局 gravity 依靠父布局的位置如Gravity.CENTER  x y 坐标值         

  1. window.setWidth(DisplayManager.dipToPixel(150));  
  2. window.setHeight(DisplayManager.dipToPixel(150));  
  3.   
  4. // 设置PopupWindow外部区域是否可触摸   
  5. window.setFocusable(true); //设置PopupWindow可获得焦点   
  6. window.setTouchable(true); //设置PopupWindow可触摸   
  7. window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸  
		window.setWidth(DisplayManager.dipToPixel(150));
		window.setHeight(DisplayManager.dipToPixel(150));
		
		// 设置PopupWindow外部区域是否可触摸
		window.setFocusable(true); //设置PopupWindow可获得焦点
		window.setTouchable(true); //设置PopupWindow可触摸
		window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
        设置其可以获得焦点,可触摸,外部区域可触摸(很重要)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值