Android UI开发第三篇——popupwindow

简介:
PopupWindow在android.widget包下,弹出窗口的形式展示。官方文档对该控件的描述是:“一个弹出窗口控件,可以用来显示任意视图(View),而且会浮动在当前 活动(activity)的顶部”。PopupWindow可以让我们实现多种自定义控件,例如:menu、alertdialog等弹窗似的View。

popupwindow需要在onCreate时初始化、通过触发事件展示出来。

public class ShowPopupWindow extends Activity implements View.OnClickListener{

 
View view;
PopupWindow pop;
Button btnShowAsDrawDown;
Button btnShowAsDrawDown1;
Button btnShowAtLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_activity);
btnShowAsDrawDown = (Button)findViewById(R.id.btnShowAsDrawDown);
btnShowAsDrawDown.setOnClickListener(this);
btnShowAsDrawDown = (Button)findViewById(R.id.btnShowAsDrawDown1);
btnShowAsDrawDown.setOnClickListener(this);
btnShowAtLocation = (Button)findViewById(R.id.btnShowAt);
btnShowAtLocation.setOnClickListener(this);
initPopupWindow();
}

 
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnShowAsDrawDown:
if(pop.isShowing())
{
pop.dismiss();
}
else
{
pop.showAsDropDown(v);
}
break;
case R.id.btnShowAsDrawDown1:
if(pop.isShowing())
{
pop.dismiss();
}
else
{
pop.showAsDropDown(v, 0, -160);
}
break;
default:
if(pop.isShowing())
{
pop.dismiss();
}
else
{
 
pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER_HORIZONTAL, 0, 0);
}
break;
}

 
 

 
}
 
private void initPopupWindow()
{
view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setOutsideTouchable(true);
view.setOnClickListener(new View.OnClickListener() {
 
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pop.dismiss();
}
});
}

 
}

 
popup_activity.xml

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="pop demo!"
/>
<Button
android:id="@+id/btnShowAsDrawDown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show as drawndown(one parameter)"
/>
<Button
android:id="@+id/btnShowAsDrawDown1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show as drawndown(three parameters)"
/>
<Button
android:id="@+id/btnShowAt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show At Location"
/>
</LinearLayout>

 
popup_window.xml

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#d3d3d3">
<Button
android:id="@+id/btn_pop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:layout_margin="10dip" />
</LinearLayout>


     本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817389 ,如需转载请自行联系原作者

相关文章
|
3天前
|
编解码 数据库 Android开发
安卓应用开发:打造高效用户界面的五大技巧
【5月更文挑战第18天】在竞争激烈的应用市场中,一个流畅且直观的用户界面(UI)对于安卓应用的成功至关重要。本文将探讨五种提升安卓应用用户界面性能的技巧,包括合理布局设计、优化资源使用、利用硬件加速、内存管理以及响应式编程。通过这些方法,开发者可以创建出既美观又高效的应用体验,从而吸引和保留用户。
|
4天前
|
JSON Android开发 数据格式
Android框架-Google官方Gson解析,android开发实验报告总结
Android框架-Google官方Gson解析,android开发实验报告总结
|
4天前
|
前端开发 Android开发
Android架构组件JetPack之DataBinding玩转MVVM开发实战(四)
Android架构组件JetPack之DataBinding玩转MVVM开发实战(四)
Android架构组件JetPack之DataBinding玩转MVVM开发实战(四)
|
4天前
|
安全 Linux Android开发
Android最强保活黑科技的最强技术实现,2024年最新阿里资深Android开发带你搞懂Framework
Android最强保活黑科技的最强技术实现,2024年最新阿里资深Android开发带你搞懂Framework
Android最强保活黑科技的最强技术实现,2024年最新阿里资深Android开发带你搞懂Framework
|
4天前
|
算法 前端开发 Android开发
Android文字基线Baseline算法的使用讲解,Android开发面试题
Android文字基线Baseline算法的使用讲解,Android开发面试题
Android文字基线Baseline算法的使用讲解,Android开发面试题
|
4天前
|
缓存 Android开发
Android插件化——高手必备的Hook技术,零基础开发android
Android插件化——高手必备的Hook技术,零基础开发android
|
4天前
|
Android开发
Android高级开发面试题以及笞案整理,实战解析
Android高级开发面试题以及笞案整理,实战解析
|
6天前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
33 0
|
6天前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
18 0
|
6天前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍
38 0