Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉) - 落日小屋 - 博客频道 - CSDN.NET

Android 4.0

Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)

分类: Android进阶2 4042人阅读 评论(2) 收藏 举报

PopupWindow是一个可以用来显示一个任意的视图的弹出窗口,他需要完全依赖layout布局。

它没什么界面,在弹出的窗口中完全显示布局中的控件。



上面两个美女头就是弹窗PopupWindow显示的内容。是两个Button。


具体实现:

注意:那三个Button不能和普通的Button一样通过findViewById()方法获得,必须首先说的Button所在的视图,View popview = layoutInflater.inflate(R.layout.poplayout, null);

我的Button在poplayout.xml中。最后通过button1 = (Button) popview.findViewById(R.id.button1)获得。

另外一点就是:不要在oncreate()中获得Button,而是像我一样在onclick方法下获得,和popupwindow一起。这一点不一定正确。

为什么要提呢?因为我在oncreate()中获得Button时,button的点击事件不能用,我也不是很清楚。那位大牛要是知道的话,可以告诉我一下。


showAtLocation(findViewById(R.id.edit_layout), Gravity.BOTTOM,0, 0);  

设置弹窗的位置:

第一个参数是弹窗父控件的布局;

第二个参数是位置如左,右,上部,下部等;

第三个参数是X方向的偏移量;

第四个参数是Y方向的偏移量。

  1. package xiaosi.popwindow;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.view.WindowManager.LayoutParams;  
  10. import android.widget.Button;  
  11. import android.widget.PopupWindow;  
  12. import android.widget.Toast;  
  13.   
  14. public class PopwindowActivity extends Activity implements OnClickListener  
  15. {  
  16.     /** Called when the activity is first created. */  
  17.     private Button button = null;  
  18.     private Button button1 = null;  
  19.     private Button button2 = null;  
  20.     private Button button3 = null;  
  21.   
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState)  
  24.     {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.         button = (Button) findViewById(R.id.button);  
  28.         button.setOnClickListener(this);  
  29.     }  
  30.   
  31.     public void onClick(View arg0)  
  32.     {  
  33.         if (arg0.getId() == R.id.button)  
  34.         {  
  35.             LayoutInflater layoutInflater = (LayoutInflater) (PopwindowActivity.this)  
  36.                     .getSystemService(LAYOUT_INFLATER_SERVICE);  
  37.             // 获取自定义布局文件poplayout.xml的视图  
  38.             View popview = layoutInflater.inflate(R.layout.poplayout, null);  
  39.             PopupWindow popWindow = new PopupWindow(popview,  
  40.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  41.             //规定弹窗的位置  
  42.             popWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM,  
  43.                     00);  
  44.             //PopupWindow里的两个Button  
  45.             button1 = (Button) popview.findViewById(R.id.button1);  
  46.             button1.setOnClickListener(this);  
  47.             button2 = (Button) popview.findViewById(R.id.button2);  
  48.             button2.setOnClickListener(this);  
  49.         }  
  50.         else if (arg0.getId() == R.id.button1)  
  51.         {  
  52.             Toast.makeText(PopwindowActivity.this"button1", Toast.LENGTH_LONG)  
  53.                     .show();  
  54.         }  
  55.         else if (arg0.getId() == R.id.button2)  
  56.         {  
  57.             Toast.makeText(PopwindowActivity.this"button2", Toast.LENGTH_LONG)  
  58.                     .show();  
  59.         }  
  60.     }  
  61. }  
package xiaosi.popwindow;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class PopwindowActivity extends Activity implements OnClickListener
{
	/** Called when the activity is first created. */
	private Button button = null;
	private Button button1 = null;
	private Button button2 = null;
	private Button button3 = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(this);
	}

	public void onClick(View arg0)
	{
		if (arg0.getId() == R.id.button)
		{
			LayoutInflater layoutInflater = (LayoutInflater) (PopwindowActivity.this)
					.getSystemService(LAYOUT_INFLATER_SERVICE);
			// 获取自定义布局文件poplayout.xml的视图
			View popview = layoutInflater.inflate(R.layout.poplayout, null);
			PopupWindow popWindow = new PopupWindow(popview,
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			//规定弹窗的位置
			popWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM,
					0, 0);
			//PopupWindow里的两个Button
			button1 = (Button) popview.findViewById(R.id.button1);
			button1.setOnClickListener(this);
			button2 = (Button) popview.findViewById(R.id.button2);
			button2.setOnClickListener(this);
		}
		else if (arg0.getId() == R.id.button1)
		{
			Toast.makeText(PopwindowActivity.this, "button1", Toast.LENGTH_LONG)
					.show();
		}
		else if (arg0.getId() == R.id.button2)
		{
			Toast.makeText(PopwindowActivity.this, "button2", Toast.LENGTH_LONG)
					.show();
		}
	}
}


poplayout.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:gravity="center_horizontal"  
  6.     android:orientation="horizontal" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/button1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="@drawable/colorframe_1" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/button2"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:background="@drawable/colorframe_3" />  
  19.   
  20. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/colorframe_1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/colorframe_3" />

</LinearLayout>

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/main"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"   
  7.     android:background="@drawable/background">  
  8.   
  9.     <Button  
  10.         android:id="@+id/button"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="点击查看效果" />  
  14.     <ImageView   
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:src="@drawable/a" />  
  18.   
  19. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/background">

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="点击查看效果" />
    <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/a" />

</LinearLayout>


以上只是用来学习之用,拿出来和大家一起分享一下。

有想要源代码的给我留言。


更多 0
4
0
查看评论
2楼 zouzhigang96 2013-08-13 17:37发表 [回复] [引用] [举报]
很不错。 谢谢楼主分享
1楼 dingxinssy 2012-03-19 11:31发表 [回复] [引用] [举报]
看了楼主不少文章,内容比较简单,我也是都是简单的掠过,呵呵 我差的还很多,想和楼主多交流下 qq 296506597
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:441662次
    • 积分:10040分
    • 排名:第316名
    • 原创:495篇
    • 转载:217篇
    • 译文:0篇
    • 评论:272条
    博客专栏
    最新评论
公司简介|招贤纳士|广告服务|银行汇款帐号|联系方式|版权声明|法律顾问|问题报告
QQ客服 微博客服 论坛反馈 服务热线:400-600-2320
京 ICP 证 070598 号
北京创新乐知信息技术有限公司 版权所有
世纪乐知(北京)网络技术有限公司 提供技术支持
江苏乐知网络技术有限公司 提供商务支持
Copyright © 1999-2012, CSDN.NET, All Rights Reserved