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. jquery easyui datebox 时间控件默认显示当前日期的实现方法

    jquery easyui datebox 时间控件默认显示当前日期的实现方法 直接class easyui-datebox后添加一个value="true"就可以

  2. ES6 - 对象

    ES6为对象带来的新特性. 对象传统的写法: let person={ 'name':'Lily', 'say':function(){ alert('hello!'); } } 1.ES6中写法更简 ...

  3. JSP显示-下拉框

    <TABLE class="table-frame" id=p1 cellSpacing=0 cellPadding=0 width="99%" alig ...

  4. C#部分---二维数组、split分割;

    二维数组定义方式: int[,] array = new int[3, 4]{ {1,2,3,4}, {3,4,5,6}, {5,6,7,8} };                  3表示,有三个一 ...

  5. C++@语句块

    #include <iostream> using namespace std; int main() { { int x=1; cout << x << endl ...

  6. Unity模型导入导出

    从3DMAX导出,参考: http://tieba.baidu.com/p/2807225555 -> 使用3dmax 2013,会自带导出 fbx 的功能 -> 从 3dmax 导出 - ...

  7. Linux下tmpfs介绍及使用

    tmpfs介绍 tmpfs是一种虚拟内存文件系统,而不是块设备.是基于内存的文件系统,创建时不需要使用mkfs等初始化它最大的特点就是它的存储空间在VM(virtual memory),VM是由lin ...

  8. 最大化 AIX 上的 Java 性能,第 2 部分: 速度需求

    http://www.ibm.com/developerworks/cn/aix/library/es-Javaperf/es-Javaperf2.html 最大化 AIX 上的 Java 性能,第 ...

  9. LINQ(LINQ to DataSet)

    http://www.cnblogs.com/SkySoot/archive/2012/08/21/2649471.html DataTable.Select()方法使用和 SQL 相似的过滤语法从 ...

  10. linux服务之httpd

    http://mirrors.cnnic.cn/apache/httpd/docs/ 英文pdf文档下载 Apache HTTP Project’s goal It is the Apache HTT ...