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 ...
随机推荐
- ZOJ 1049 I Think I Need a Houseboat
原题链接 题目大意:Fred想在Louisiana买一套房子,但是堤坝不牢固,每年都要被河水侵蚀50平方英里.题目给出他豪宅的坐标,要求他被迫移民搬迁的年份. 解法:也没什么好说的,先求出两点间的距离 ...
- vim 被墙
我只能说,呵呵.还好这货可以进. http://vim.wendal.net/
- uboot完全手册---14
1. u-boot介绍 本次移植采用的是U-Boot-1.2.0版本. 3. U-Boot源码分析 3.1 源码入口的解释 可能大多数的同学上网查资料后都了解到,stage1阶段的启动代码,主要就在s ...
- 在JSP页面下使用AJAX实现用户名存在的检测
<script type="text/javascript"> function init(){ document.getElementById ...
- URAL 1076 Trash Trash(最大权匹配)
Trash Time limit: 1.0 secondMemory limit: 64 MB You were just hired as CEO of the local junkyard.One ...
- Attention and Augmented Recurrent Neural Networks
Attention and Augmented Recurrent Neural Networks CHRIS OLAHGoogle Brain SHAN CARTERGoogle Brain Sep ...
- Doing well in your courses ---- a guide by Andrej Karpathy
Doing well in your courses a guide by Andrej Karpathy Here is some advice I would give to younger st ...
- matlab 工具之各种降维方法工具包,下载及使用教程,有PCA, LDA, 等等。。。
最近跑深度学习,提出的feature是4096维的,放到我们的程序里,跑得很慢,很慢.... 于是,一怒之下,就给他降维处理了,但是matlab 自带的什么pca( ), princomp( )函数, ...
- CentOS 6 DEPRECATED
Created by YEISON CAMARGO, last modified by John Boteler on 2016.02.02 Go to start of metadata Abo ...
- GPU的革命
CUDA 线程执行模型分析(一)招兵------ GPU的革命 CUDA 线程执行模型分析(二)大军未动粮草先行------GPU的革命 CUDA硬件实现分析(一)------安营扎寨-----GPU ...