如图是效果图

PopupWindow简单使用-LMLPHP

当点击 “点我”的按钮是   会弹出 如图的 弹窗

补充为PopupWindow设置一个显示动画和消失的动画

先在anim的文件下分别设置显示和消失的动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate
android:duration="500"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="100%"
android:toYDelta="0%" />
<!-- <alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500"
android:startOffset="500"/> --> </set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="100%"
android:duration="500" />
</set>

再在style内写其他的

    <style name="popwin_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow</item>
<item name="android:windowExitAnimation">@anim/pophide</item>
</style>
            //创建PopupWindow实例,同时传入弹出窗口的显示高度和宽度以及是否设置焦点
popupWindow = new PopupWindow(infView,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,true);
//如果不设置PopupWindow背景,无论是点击外部区域还是Back键都没法dismiss弹窗
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//设置显示的动画
popupWindow.setAnimationStyle(R.style.popwin_anim_style);

主代码如下    布局xml  就是只有一个 Button

 package org.xml.popdemo;

 import ogg.huanxin.huadong.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainPopWindow extends Activity implements OnClickListener {
private Button popButton;
private PopWindowForAttr popWindowForAttr; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main_popwindow);
popButton=(Button)super.findViewById(R.id.popbutton);
popButton.setOnClickListener(this);
popWindowForAttr = new PopWindowForAttr(this);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.popbutton:
popWindowForAttr.showAsDropDown(v);
break;
default:
break; }
}
}
 package org.xml.popdemo;

 import ogg.huanxin.huadong.R;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.PopupWindow.OnDismissListener; public class PopWindowForAttr implements OnClickListener, OnDismissListener {
private Context context;
private PopupWindow popupWindow;
private Button addCart, doCart;
private final int AddReduce = 1;
private TextView Popreduce, Popnum, Popadd;
private ImageButton deleteButton; public PopWindowForAttr(Context context) {
this.context = context;
// 自定义的一个控件作为显示内容
View contentView = LayoutInflater.from(context).inflate(
R.layout.popupwindowdemo, null);
//
addCart = (Button) contentView.findViewById(R.id.addCart);
doCart = (Button) contentView.findViewById(R.id.doCart);
Popreduce = (TextView) contentView.findViewById(R.id.pop_reduce);
Popnum = (TextView) contentView.findViewById(R.id.pop_num);
Popadd = (TextView) contentView.findViewById(R.id.pop_add);
deleteButton = (ImageButton) contentView.findViewById(R.id.delete);
// 各组件绑定事件
addCart.setOnClickListener(this);
doCart.setOnClickListener(this);
Popreduce.setOnClickListener(this);
Popadd.setOnClickListener(this);
deleteButton.setOnClickListener(this);
// 将加载的视图view载入PopubWindow,并且设置popupwindow这个组件的动画效果
popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, true);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// 当popWindow消失时的监听
popupWindow.setOnDismissListener(this);
} public void showAsDropDown(View v) {
// showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
// showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
// showAtLocation(View parent, int gravity, int x, int
// y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
popupWindow.showAtLocation(v, Gravity.BOTTOM, 100, 100);
// 设置setFocusable(true),要不然点击弹窗其他地方以及返回键,弹窗都不会退出
// 也才能让popupWindow里面的布局控件获得点击的事件,否则就被它的父亲view给拦截了
popupWindow.setFocusable(true);
//这个方法时设置popupWindow以外的区域可以相应触摸事件,比如我们重写了触摸事件去做一些别的操作,但首先Focusable是true。
popupWindow.setOutsideTouchable(true);
popupWindow.update(); } // 销毁
@Override
public void onDismiss() {
// TODO Auto-generated method stub
popupWindow.dismiss();
} @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.addCart:
Toast.makeText(context, "添加到购物车", Toast.LENGTH_SHORT).show();
break;
case R.id.doCart:
Toast.makeText(context, "立即购买", Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
onDismiss();
break;
case R.id.pop_reduce:
if (!Popnum.getText().toString().equals("1")) {
String num_ReduceString = Integer.valueOf(Popnum.getText()
.toString()) - AddReduce + "";
Popnum.setText(num_ReduceString);
} else {
Toast.makeText(context, "您买入的数量不能低于1", Toast.LENGTH_SHORT)
.show();
}
break;
case R.id.pop_add:
if (!Popnum.getText().toString().equals("1000")) {
String numString = Integer.valueOf(Popnum.getText().toString())
+ AddReduce + "";
Popnum.setText(numString);
}
break;
default:
break;
}
}
}
 <?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:background="#FFFFFF"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="10dp" > <ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:src="@drawable/d" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="现售价:¥49.00"
android:textSize="15sp" /> <TextView
android:id="@+id/kucun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="4dp"
android:text="库存为:987"
android:textSize="12sp" />
</LinearLayout> <ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/delete"
android:layout_weight="1"
android:background="@null"
android:padding="4dp"
android:src="@drawable/delete" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#eee"
android:orientation="horizontal" > <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="1dp"
android:text="购买数量" /> <TextView
android:id="@+id/pop_reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="10dp"
android:text="-"
android:color="#000000" /> <TextView
android:id="@+id/pop_num"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="1"
android:textColor="#000000" /> <TextView
android:id="@+id/pop_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="10dp"
android:text="+"
android:textColor="#000000" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:id="@+id/addCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/png_3"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:text="加入购物车"
android:textColor="@android:color/white" /> <Button
android:id="@+id/doCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/png_3"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:text="立即购买"
android:textColor="@android:color/white" />
</LinearLayout> </LinearLayout>

另外一种常见的方式  当点击一个下拉箭头  会出现的一个popowind 如下拉选择

下拉选择

1 假如listview的item中有Button,ImageButton,CheckBox等会强制获取焦点的view,这时listview的item无法获取焦点,从而无法被点击

解决方法    在item的根布局增加以下的属性android:descendantFocusability="blocksDescendants"设置后,Button获取焦点,item中的其他控件页可以获取焦点了

如下是主要代码

 package com.ithello.xiala;

 import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView; /**
* 实现下拉选择的控件
*
* @author Administrator
*
*/
public class MainActivity extends Activity implements OnClickListener {
/**文本编辑框*/
private EditText editText;
/**下拉的图标点击可以出现弹窗*/
private ImageView imageView;
/**弹窗中的list数据*/
private List<String> list = new ArrayList<String>();
/***/
private ListView listView;
/**弹窗PopupWindow*/
private PopupWindow popupWindow;
/**弹窗PopupWindow的默认高度*/
private int popupWindowHight = 300; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
setView();
setData();
} private void setView() {
// TODO Auto-generated method stub
editText = (EditText) findViewById(R.id.edt_main);
imageView = (ImageView) findViewById(R.id.iv); imageView.setOnClickListener(this);
} private void setData() {
// TODO Auto-generated method stub
// 初始化数据
for (int i = 0; i < 15; i++) {
list.add(900000 + i + "");
} listView = new ListView(this);
listView.setVerticalScrollBarEnabled(false);// 隐藏listview的滚动条
listView.setBackgroundColor(Color.parseColor("#E6E6E6"));
listView.setDivider(new ColorDrawable(Color.WHITE));
         listView.setDividerHeight(1);
MyAdapter adapter = new MyAdapter();
listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.d("jiejie", arg2 + "");
editText.setText(list.get(arg2));
popupWindow.dismiss();
}
});
} /**
* 显示一个PopupWindow
*/
private void showNumPopu() {
if (popupWindow == null) {
popupWindow = new PopupWindow(listView, editText.getWidth(),
popupWindowHight);
}
// 要让其中的view获取焦点,必须设置为true
popupWindow.setFocusable(true);
// 还必须设置一个背景图片
popupWindow.setBackgroundDrawable(new BitmapDrawable());
// 设置点击外部点击消失
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(editText, 0, 0);
} @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.iv:
showNumPopu();
break; default:
break;
}
} // list的适配器
private class MyAdapter extends BaseAdapter { @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public View getView(final int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
final View view = View.inflate(MainActivity.this,
R.layout.item_list, null);
TextView tv_number = (TextView) view.findViewById(R.id.item_text);
ImageView iv_delete = (ImageView) view
.findViewById(R.id.item_image);
tv_number.setText(list.get(position));
iv_delete.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("jiejie", "position " + position);
list.remove(position);
notifyDataSetChanged();
int listviewHeight = view.getHeight() * list.size();
popupWindow.update(
editText.getWidth(),
listviewHeight > popupWindowHight ? popupWindowHight
: listviewHeight); if (list.size() == 0) {
imageView.setVisibility(View.INVISIBLE);
popupWindow.dismiss();
} }
});
return view;
} }
}
<?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:background="#000"
android:orientation="vertical" > <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" > <EditText
android:id="@+id/edt_main"
android:layout_width="180dp"
android:layout_height="40dp"
android:hint=""
android:background="#fff"
android:paddingRight="20dp" /> <ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/edt_main"
android:layout_centerVertical="true"
android:contentDescription="@null"
android:padding="5dp"
android:src="@drawable/spread" />
</RelativeLayout> </LinearLayout>
05-27 07:09