android学习笔记17——对话框(PopupWindow)
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)的更多相关文章
- android学习笔记16——对话框
android支持丰富的对话框,常用4中对话框: 1.AlertDialog: 2.ProgressDialog:进度对话框,这个对话框只是对进度条的封装 3.DatePickerDialog:日期选 ...
- android学习笔记19——对话框(DatePickerDialog、TimePickerDialog)
DatePickerDialog.TimePickerDialog ==> DatePickerDialog.TimePickerDialog功能.用法都比较简单,操作步骤: 1.通过new关键 ...
- Android学习笔记(17):文本框TextView类
TextView继承自View.用于显示文本.它有很多的子类,掌握其属性是非常重要的. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...
- Android学习笔记17:单项选择RadioButton和多项选择CheckBox的使用
请参见 http://www.android100.org/html/201406/05/19495.html
- 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge
目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...
- Android学习笔记进阶17之LinearGradient
具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- 【转】 Pro Android学习笔记(五七):Preferences(1):ListPreference
目录(?)[-] 例子1ListPreference小例子 定义一个preferences XML文件 继承PreferenceActivity 用户定制偏好的读取 第一次运行时设置缺省值 设置Cat ...
- 【转】 Pro Android学习笔记(五六):配置变化
目录(?)[-] Activity的destorycreate过程 Fragment的destorycreate过程 onSaveInstanceState saveFragmentInstanceS ...
随机推荐
- (基础篇)PHP与JavaScript交互
一.了解javascript Javascript是脚本编程语言,支持WEB应用程序的客户端和服务器端构件的开发,在WEB系统中得到了非常广泛的应用. 1. 什么是javascript Javascr ...
- 解决Eclipse Pydev中import时报错:Unresolved import
在安装 图像处理工具包 mahotas 后,在eclipse中尝试import mahotas时,出现Unresolved import错误,按快捷无法自动生成代码提示 但是,程序运行时可以通过,在命 ...
- ZOJ 1037 Gridland
原题链接 题目大意:给出一个格子图,求走完所有节点的最短路径距离. 解法:简单啊,如果都是奇数,可以走一次斜边,其他情况就是长*宽. 参考代码: #include <stdio.h> in ...
- TextView中的图文混排
ImageSpan imageSpanMenu1 = new ImageSpan(activity,menuResId1); SpannableString contentMenu1 = new Sp ...
- System program problem detected 解决
每次开机都出现:System program problem detected 管理员权限打开:/etc/default/apport su root vim /etc/default/app ...
- leetcode 115 Distinct Subsequences ----- java
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- POJ3308 Paratroopers(网络流)(最小割)
Paratroopers Time Limit: 1000MS Memory Limit: 655 ...
- php strtotime 和 date 日期操作
time()得到的数值是1970-1-1到现在的秒数,strtotime("now")也是,两个值是相同的. http://doc.thinkphp.cn/manual/syste ...
- Verilog
1.位拼接运算符{}
- Sklearn库例子2:分类——线性回归分类(Line Regression )例子
线性回归:通过拟合线性模型的回归系数W =(w_1,…,w_p)来减少数据中观察到的结果和实际结果之间的残差平方和,并通过线性逼近进行预测. 从数学上讲,它解决了下面这个形式的问题: Lin ...