Android开发系列(二十六):使用PopupWindow创建对话框风格的窗口
来源:互联网 发布:场景建模软件 编辑:程序博客网 时间:2024/04/29 01:26
创建对话框风格的窗口很简单,需要步骤:
1、调用PopupWindow的构造器创建PopupWindow对象
2、调用PopupWindow的showAsDropDown(View v)作为v组件的下拉组件显示出来:或调用PopupWindow的showAtLocation()方法将PopupWindow在指定位置显示出来。
首先,我们创建一个Android项目,然后编辑main.xml文件:
<?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"android:gravity="center_horizontal"><Button android:id="@+id/bn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="弹出Popup窗口" /></LinearLayout>我们定义了一个按钮,用来打开Popup对话框风格的窗口
然后,我们在主界面编辑java代码:PopupWindowTest.java
import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.PopupWindow;public class PopupWindowTest extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);// 装载R.layout.popup对应的界面布局View root = this.getLayoutInflater().inflate(R.layout.popup, null);// 创建PopupWindow对象final PopupWindow popup = new PopupWindow(root, 280, 360);Button button = (Button) findViewById(R.id.bn);button.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){// 以下拉方式显示。//popup.showAsDropDown(v);//将PopupWindow显示在指定位置popup.showAtLocation(findViewById(R.id.bn), Gravity.CENTER, 20,20);}});// 获取PopupWindow中的关闭按钮。root.findViewById(R.id.close).setOnClickListener(new View.OnClickListener(){public void onClick(View v){// 关闭PopupWindowpopup.dismiss(); //负责销毁、隐藏PopupWindow的关键代码}});}}
<?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"android:gravity="center_horizontal"android:background="#B1C9E4"><ImageView android:layout_width="240dp"android:layout_height="wrap_content"android:src="@drawable/java"/><Buttonandroid:id="@+id/close"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="关闭" /></LinearLayout>
我们来看一下效果:
0 0
- Android开发系列(二十六):使用PopupWindow创建对话框风格的窗口
- 对话框风格的窗口以及使用PopupWindow创建对话框风格窗口
- 探索Popupwindow-对话框风格的窗口(
- Android对话框风格的窗口
- Android开发系列(二十七):使用ProgressDialog创建进度对话框
- Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框
- 【android开发】手机应用管理器的实现之实现popupWindow类对话框(二)
- 【android开发】手机应用管理器的实现之实现popupWindow类对话框(二)
- android典型代码系列(二十五)------popupwindow的使用
- 对话框风格的窗口
- android典型代码系列(二十六)------App widget的使用
- Android--从零单排系列(6)--相对应对话框popupwindow的优势和使用
- Android 对话框系列——类似IOS风格的对话框
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)!!!!
- Solr部署如何启动
- eclipse validating 很慢
- 五分钟理解一致性哈希算法(consistent hashing)
- 第10周项目3-血型统计
- vc6.0 打开文件的时候出现Microsoft Visual C++:MSDEV.EXE 应用程序错误
- Android开发系列(二十六):使用PopupWindow创建对话框风格的窗口
- Pro Android学习笔记(一四二):触摸屏(1):MotionEvents
- 德众金融:阳光暖暖的
- CentOS7下初始化PostgreSQL
- 迈向架构师的第一步
- 银滩旅游度假区宫家岛旅游开发项目
- 用linux命令touch修改文件及其子文件夹内文件的时间属性
- 第十周项目一程序填充与阅读
- iOS8 AlertView 改变 引入了UIAlertController