android下面popwindow的使用

PopupWindow是阻塞对话框,只有在外部线程或者 PopupWindow本身做退出操作才行。PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。

1.使用 popwindow实现如下效果:

2.代码如下:MainActivity.java

publicclass MainActivity extends Activity {

private Button button;

@Override

publicvoid onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//新建一个按钮

button = (Button) findViewById(R.id.button);

//按钮的监听事件

button.setOnClickListener(new OnClickListener()

{

publicvoid onClick(View v)

{

// 新建一个popwindow,并显示里面的内容

PopupWindow popupWindow = makePopupWindow(MainActivity.this);

//int[] xy = new int[2];

//button.getLocationOnScreen(xy);

//popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,xy[0]/2,xy[1]+button.getWidth());

//popwindow与按钮之间的相对位置

popupWindow.showAsDropDown(button,2, 5);

}

});

}

// 创建一个包含自定义viewPopupWindow

private PopupWindow makePopupWindow(Context cx)

{

PopupWindow window;

window = new PopupWindow(cx);

/*ListView listview = new ListView(this);

listview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

listview.setAdapter(new ArrayAdapter<String>(cx, android.R.layout.simple_expandable_list_item_1,getData()));

*/

//View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null);

//window.setContentView(contentView);

Button b1 = new Button(this);

b1.setText("first");

b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

Button b2 = new Button(this);

b2.setText("Second");

b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

LinearLayout linearLayout = new LinearLayout(this);

linearLayout.addView(b1);

linearLayout.addView(b2);

//linearLayout.addView(listview);

linearLayout.setOrientation(LinearLayout.VERTICAL);

window.setContentView(linearLayout); //选择布局方式

//设置popwindow的背景图片

window.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_pop_pressed));

//设置popwindow的高和宽

window.setWidth(70);

window.setHeight(320);

// 设置PopupWindow外部区域是否可触摸

window.setFocusable(true); //设置PopupWindow可获得焦点

window.setTouchable(true); //设置PopupWindow可触摸

window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸

return window;

}

//构造数组的函数

private List<String> getData(){

List<String> data = new ArrayList<String>();

data.add("数学");

data.add("英语");

data.add("语文");

data.add("历史");

data.add("政治");

//data.add("美术");

//data.add("体育");

return data;

}

@Override

publicboolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

returntrue;

}

}

package mtpop.window.main;

import android.app.Activity;

import android.content.Context;

import android.content.res.Resources;

import android.os.Bundle;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup.LayoutParams;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.PopupWindow;

import android.widget.PopupWindow.OnDismissListener;

import android.widget.TextView;

public class MainActivity extends Activity

{

private static final String TAG = "MainActivity";

private Button button;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

button = (Button) findViewById(R.id.button);

button.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

// 显示 popupWindow

PopupWindow popupWindow = makePopupWindow(MainActivity.this);

int[] xy = new int[2];

button.getLocationOnScreen(xy);

popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,-xy[0]/2,xy[1]+button.getWidth());

//popupWindow.showAsDropDown(button,0, 0);

}

});

}

// 创建一个包含自定义view的PopupWindow

private PopupWindow makePopupWindow(Context cx)

{

PopupWindow window;

window = new PopupWindow(cx);

//View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null);

//window.setContentView(contentView);

Button b1 = new Button(this);

b1.setText("first");

b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

Button b2 = new Button(this);

b2.setText("Second");

b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

LinearLayout linearLayout = new LinearLayout(this);

linearLayout.addView(b1);

linearLayout.addView(b2);

linearLayout.setOrientation(LinearLayout.VERTICAL);

window.setContentView(linearLayout);

window.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_bg));

window.setWidth(DisplayManager.dipToPixel(150));

window.setHeight(DisplayManager.dipToPixel(150));

// 设置PopupWindow外部区域是否可触摸

window.setFocusable(true); //设置PopupWindow可获得焦点

window.setTouchable(true); //设置PopupWindow可触摸

window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸

return window;

}

}


3. main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:orientation="horizontal" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Title" />


</LinearLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="click" />

</RelativeLayout>

</LinearLayout>

<?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" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@android:color/darker_gray"

android:orientation="horizontal" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="Title" />

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="click" />

</LinearLayout>

</LinearLayout>


注意:

*新建一个popupWindow弹出框popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待,

*这和AlertDialog不同哦,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。 *

******************************************************************

1.PopupWindow的隐藏

// 隐藏菜单

publicvoid dismiss() {

Window.dismiss();

}final PopupWindow window = mPageStatWin;

if(null != window && window.isShowing()) {

win.dismiss();

}


2.Popupwindow的显示及位置设置

window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10);

第一个参数指定PopupWindow的锚点view,即依附在哪个view上。

第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。

//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
window.showAsDropDown(anchor);
//xoff,yoff
基于anchor的左下角进行偏移。
window.showAsDropDown(anchor, xoff, yoff);

如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。

*popupWindow.showAsDropDown(Viewview)弹出对话框,位置在紧挨着view组件

*showAsDropDown(Viewanchor,intxoff,intyoff)弹出对话框,位置在紧挨着view组件,xy代表着偏移量

*showAtLocation(Viewparent,intgravity,intx,inty)弹出对话框

*parent父布局gravity依靠父布局的位置如Gravity.CENTERxy坐标值

//设置popwindow的高和宽

window.setWidth(70);

window.setHeight(320);

// 设置PopupWindow外部区域是否可触摸

window.setFocusable(true); //设置PopupWindow可获得焦点

window.setTouchable(true); //设置PopupWindow可触摸

window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸

设置其可以获得焦点,可触摸,外部区域可触摸(很重要)

更多相关文章

  1. Android开源SlidingMenu的使用
  2. Android圆形进度条颜色的设置
  3. android之recyclerview的基本使用
  4. activity配置信息详解
  5. Android的ImageSwitcher和TextSw-android学习之旅(三十四)
  6. Android(安卓)Studio 自定义设置注释模板——诺诺"涂鸦"记忆
  7. Android中dip(dp)与px之间单位转换 dip2px dp转px 无context算法
  8. Android(安卓)Spanned实现TextView富文本效果(1)--文字
  9. 【原】android本地推送

随机推荐

  1. 转载:Android(安卓)常用代码集合
  2. Android(安卓)Toast提示封装实例代码
  3. android标题栏中添加返回按钮
  4. Android(安卓)Studio基础学习(入门五)——
  5. Android动态设置android:drawableLeft|Ri
  6. android AlertDialog中EditText无法显示
  7. 使用Android(安卓)Studio遇到的一些常见
  8. android中涉及的字体修改
  9. Handler与Android进程管理
  10. Android(安卓)开发日常积累