PopupWindow

==>

PopupWindow可创建类似对话框的窗口,使用其创建对话框窗口的操作步骤:

  1.调用PopupWindow构造器构造PopupWindow对象;

  2.调用PopupWindow的showAsDropDown(View v)将PopupWindow作为v组件的下拉组件显示出来;

     或调用PopupWindow的showAtLocation方法将PopupWindow在指定位置显示出来。

注意:

AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(也可以通过设置WindowManager参数来改变位置);

而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。

实例如下:

布局文件==》
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <Button
android:id="@+id/btnTest"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="110"/> </LinearLayout> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:id="@+id/header"
android:background="@drawable/eighteen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <Button
android:id="@+id/btnclose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭"/>
</LinearLayout> 代码实现==》
package com.example.mypopupwindow; import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow; public class MainActivity extends Activity
{ @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 装载R.layout.acitivity_main对应的界面布局
View root = this.getLayoutInflater().inflate(R.layout.popup, null);
// 创建PopupWindow对象
final PopupWindow popup = new PopupWindow(root, 500, 500); Button btn = (Button) this.findViewById(R.id.btnTest);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 以下拉方式显示
popup.showAsDropDown(v);
// 将PopupWindow显示在指定位置
popup.showAtLocation(findViewById(R.id.btnTest), Gravity.CENTER_HORIZONTAL, 200, 200);
}
});
root.findViewById(R.id.btnclose).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
popup.dismiss();
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

运行效果:略

android学习笔记17——对话框(PopupWindow)的更多相关文章

  1. android学习笔记16——对话框

    android支持丰富的对话框,常用4中对话框: 1.AlertDialog: 2.ProgressDialog:进度对话框,这个对话框只是对进度条的封装 3.DatePickerDialog:日期选 ...

  2. android学习笔记19——对话框(DatePickerDialog、TimePickerDialog)

    DatePickerDialog.TimePickerDialog ==> DatePickerDialog.TimePickerDialog功能.用法都比较简单,操作步骤: 1.通过new关键 ...

  3. Android学习笔记(17):文本框TextView类

    TextView继承自View.用于显示文本.它有很多的子类,掌握其属性是非常重要的. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...

  4. Android学习笔记17:单项选择RadioButton和多项选择CheckBox的使用

    请参见 http://www.android100.org/html/201406/05/19495.html

  5. 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge

    目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...

  6. Android学习笔记进阶17之LinearGradient

    具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...

  7. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  8. 【转】 Pro Android学习笔记(五七):Preferences(1):ListPreference

    目录(?)[-] 例子1ListPreference小例子 定义一个preferences XML文件 继承PreferenceActivity 用户定制偏好的读取 第一次运行时设置缺省值 设置Cat ...

  9. 【转】 Pro Android学习笔记(五六):配置变化

    目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...

随机推荐

  1. 这是高手最常用的第一快捷组合键 - imsoft.cnblogs

    1.锁定电脑 2.要找电脑上的文件时,一般人会先找到“我的电脑”,然后点击打开,而高手总是很酷的,轻轻按下键盘上的Windows键不放然后再按E键,直接打开电脑的资源管理器,而一般人还在慢慢寻找“我的 ...

  2. int integer string间的转换

    1.int-->Integer new Integer(i); 2.Integer-->int Integer i = new Integer(1); int k = i.intValue ...

  3. Windows IP安全策略。

    一直在遗憾Windows下没有一个如Linux小Iptables一样的工具,能够严格的管控机器的访问限制. 后面突然看到一个叫Ipsec在Windows感觉还不错.以命令行的方式进行定义的话在多台服务 ...

  4. Java-->把txt中的所有字符按照码表值排序

    --> List 列表中的自动添加的多余空间长度该怎么去除呢?...(已解决,是char 数组中的空字符) package com.dragon.java.filesort; import ja ...

  5. jQuery 事件 - load() 方法

    例子:$("img").load(function(){ $("div").text("Image loaded"); }); 定义和用法 ...

  6. web标准的可用性和可访问性

    在Web前端开发界,有三个词经常被提及:可用性(Usability).可访问性(Accessibility)和可维护性(Maintainability). 可用性指的是:产品是否容易上手,用户能否完成 ...

  7. Hibernate控制insert\update语句

  8. Get access to Servlet

    import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class LoginServlet extend ...

  9. 账户切换[转自vbird]

    我们都是使用一般账号登陆系统的,等有需要进行系统维护或软件升级时才转为 root 的身份来动作. 那如何让一般使用者转变身份成为 root 呢?主要有两种方式喔: 以『 su - 』直接将身份变成 r ...

  10. CSS Position 定位属性介绍

    1.介绍 1.1 说明 Position 属性:规定元素的定位类型.即元素脱离文档流的布局,在页面的任意位置显示. 1.2 主要的值 ①absolute :绝对定位:脱离文档流的布局,遗留下来的空间由 ...