Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)
2012-03-10 15:49
387 查看
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方向的偏移量。
poplayout.xml
main.xml
以上只是用来学习之用,拿出来和大家一起分享一下。
有想要源代码的给我留言。
它没什么界面,在弹出的窗口中完全显示布局中的控件。
上面两个美女头就是弹窗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方向的偏移量。
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
<?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
<?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>
以上只是用来学习之用,拿出来和大家一起分享一下。
有想要源代码的给我留言。
相关文章推荐
- Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)
- Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)
- PopupWindow进阶用法——android上实现类似UCweb的自定义menu,完全模拟系统事件
- Android开发笔记(一百二十一)列表弹窗PopupMenu和ListPopupWindow
- Android的PopupWindow弹窗(以常见的分享界面为例)
- Android开发之PopupWindow仿QQ自定义弹窗
- (转)Android开发技术学习之popupwindow的弹窗实现
- Android弹窗二则: PopupWindow和AlertDialog
- Android开发技术学习之popupwindow的弹窗实现
- Android PopupWindow 弹窗背景半透明,设置最大高度
- Android自定义View之popupwindow进阶封装:高仿ios “item动画弹出”效果的popupwindow。
- Android开发技术学习之popupwindow的弹窗实现
- android 使用PopupWindow弹窗时,挡住了软键盘
- 【Android - 进阶】之PopupWindow的使用
- Android开发笔记(一百二十一)列表弹窗PopupMenu和ListPopupWindow
- Android开发笔记(一百二十一)列表弹窗PopupMenu和ListPopupWindow
- Android PopWindow 弹窗
- android PopupWindow和Activity
- Android Popupwindow使用问题
- android UI进阶之弹窗的使用--实现通讯录的弹窗效果