如图是效果图
当点击 “点我”的按钮是 会弹出 如图的 弹窗
补充为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
1 package org.xml.popdemo; 2 3 import ogg.huanxin.huadong.R; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 10 public class MainPopWindow extends Activity implements OnClickListener { 11 private Button popButton; 12 private PopWindowForAttr popWindowForAttr; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 // TODO Auto-generated method stub 17 super.onCreate(savedInstanceState); 18 super.setContentView(R.layout.main_popwindow); 19 popButton=(Button)super.findViewById(R.id.popbutton); 20 popButton.setOnClickListener(this); 21 popWindowForAttr = new PopWindowForAttr(this); 22 } 23 24 @Override 25 public void onClick(View v) { 26 // TODO Auto-generated method stub 27 switch (v.getId()) { 28 case R.id.popbutton: 29 popWindowForAttr.showAsDropDown(v); 30 break; 31 default: 32 break; 33 34 } 35 } 36 }
1 package org.xml.popdemo; 2 3 import ogg.huanxin.huadong.R; 4 import android.app.ActionBar.LayoutParams; 5 import android.content.Context; 6 import android.graphics.Color; 7 import android.graphics.drawable.ColorDrawable; 8 import android.view.Gravity; 9 import android.view.LayoutInflater; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 import android.widget.ImageButton; 14 import android.widget.PopupWindow; 15 import android.widget.TextView; 16 import android.widget.Toast; 17 import android.widget.PopupWindow.OnDismissListener; 18 19 public class PopWindowForAttr implements OnClickListener, OnDismissListener { 20 private Context context; 21 private PopupWindow popupWindow; 22 private Button addCart, doCart; 23 private final int AddReduce = 1; 24 private TextView Popreduce, Popnum, Popadd; 25 private ImageButton deleteButton; 26 27 public PopWindowForAttr(Context context) { 28 this.context = context; 29 // 自定义的一个控件作为显示内容 30 View contentView = LayoutInflater.from(context).inflate( 31 R.layout.popupwindowdemo, null); 32 // 33 addCart = (Button) contentView.findViewById(R.id.addCart); 34 doCart = (Button) contentView.findViewById(R.id.doCart); 35 Popreduce = (TextView) contentView.findViewById(R.id.pop_reduce); 36 Popnum = (TextView) contentView.findViewById(R.id.pop_num); 37 Popadd = (TextView) contentView.findViewById(R.id.pop_add); 38 deleteButton = (ImageButton) contentView.findViewById(R.id.delete); 39 // 各组件绑定事件 40 addCart.setOnClickListener(this); 41 doCart.setOnClickListener(this); 42 Popreduce.setOnClickListener(this); 43 Popadd.setOnClickListener(this); 44 deleteButton.setOnClickListener(this); 45 // 将加载的视图view载入PopubWindow,并且设置popupwindow这个组件的动画效果 46 popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, 47 LayoutParams.WRAP_CONTENT, true); 48 // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 49 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 50 // 当popWindow消失时的监听 51 popupWindow.setOnDismissListener(this); 52 } 53 54 public void showAsDropDown(View v) { 55 // showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移 56 // showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移 57 // showAtLocation(View parent, int gravity, int x, int 58 // y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移 59 popupWindow.showAtLocation(v, Gravity.BOTTOM, 100, 100); 60 // 设置setFocusable(true),要不然点击弹窗其他地方以及返回键,弹窗都不会退出 61 // 也才能让popupWindow里面的布局控件获得点击的事件,否则就被它的父亲view给拦截了 62 popupWindow.setFocusable(true); 63 //这个方法时设置popupWindow以外的区域可以相应触摸事件,比如我们重写了触摸事件去做一些别的操作,但首先Focusable是true。 64 popupWindow.setOutsideTouchable(true); 65 popupWindow.update(); 66 67 } 68 69 // 销毁 70 @Override 71 public void onDismiss() { 72 // TODO Auto-generated method stub 73 popupWindow.dismiss(); 74 } 75 76 @Override 77 public void onClick(View arg0) { 78 // TODO Auto-generated method stub 79 switch (arg0.getId()) { 80 case R.id.addCart: 81 Toast.makeText(context, "添加到购物车", Toast.LENGTH_SHORT).show(); 82 break; 83 case R.id.doCart: 84 Toast.makeText(context, "立即购买", Toast.LENGTH_SHORT).show(); 85 break; 86 case R.id.delete: 87 onDismiss(); 88 break; 89 case R.id.pop_reduce: 90 if (!Popnum.getText().toString().equals("1")) { 91 String num_ReduceString = Integer.valueOf(Popnum.getText() 92 .toString()) - AddReduce + ""; 93 Popnum.setText(num_ReduceString); 94 } else { 95 Toast.makeText(context, "您买入的数量不能低于1", Toast.LENGTH_SHORT) 96 .show(); 97 } 98 break; 99 case R.id.pop_add: 100 if (!Popnum.getText().toString().equals("1000")) { 101 String numString = Integer.valueOf(Popnum.getText().toString()) 102 + AddReduce + ""; 103 Popnum.setText(numString); 104 } 105 break; 106 default: 107 break; 108 } 109 } 110 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#FFFFFF" 6 android:orientation="vertical" 7 android:paddingLeft="15dp" 8 android:paddingRight="15dp" > 9 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:layout_gravity="center_vertical" 14 android:layout_marginTop="10dp" > 15 16 <ImageView 17 android:layout_width="50dp" 18 android:layout_height="50dp" 19 android:scaleType="fitCenter" 20 android:src="@drawable/d" /> 21 22 <LinearLayout 23 android:layout_width="wrap_content" 24 android:layout_height="match_parent" 25 android:layout_marginLeft="10dp" 26 android:layout_weight="1" 27 android:orientation="vertical" > 28 29 <TextView 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:gravity="center_vertical" 33 android:text="现售价:¥49.00" 34 android:textSize="15sp" /> 35 36 <TextView 37 android:id="@+id/kucun" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:gravity="center_vertical" 41 android:paddingTop="4dp" 42 android:text="库存为:987" 43 android:textSize="12sp" /> 44 </LinearLayout> 45 46 <ImageButton 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:id="@+id/delete" 50 android:layout_weight="1" 51 android:background="@null" 52 android:padding="4dp" 53 android:src="@drawable/delete" /> 54 </LinearLayout> 55 56 <LinearLayout 57 android:layout_width="wrap_content" 58 android:layout_height="wrap_content" 59 android:background="#eee" 60 android:orientation="horizontal" > 61 62 <TextView 63 android:layout_width="0dp" 64 android:layout_height="wrap_content" 65 android:layout_weight="1" 66 android:padding="1dp" 67 android:text="购买数量" /> 68 69 <TextView 70 android:id="@+id/pop_reduce" 71 android:layout_width="wrap_content" 72 android:layout_height="wrap_content" 73 android:paddingBottom="10dp" 74 android:paddingLeft="12dp" 75 android:paddingRight="12dp" 76 android:paddingTop="10dp" 77 android:text="-" 78 android:color="#000000" /> 79 80 <TextView 81 android:id="@+id/pop_num" 82 android:layout_width="60dp" 83 android:layout_height="wrap_content" 84 android:gravity="center" 85 android:padding="10dp" 86 android:text="1" 87 android:textColor="#000000" /> 88 89 <TextView 90 android:id="@+id/pop_add" 91 android:layout_width="wrap_content" 92 android:layout_height="wrap_content" 93 android:paddingBottom="10dp" 94 android:paddingLeft="12dp" 95 android:paddingRight="12dp" 96 android:paddingTop="10dp" 97 android:text="+" 98 android:textColor="#000000" /> 99 </LinearLayout> 100 101 <LinearLayout 102 android:layout_width="wrap_content" 103 android:layout_height="wrap_content" > 104 105 <Button 106 android:id="@+id/addCart" 107 android:layout_width="wrap_content" 108 android:layout_height="wrap_content" 109 android:layout_weight="1" 110 android:background="@drawable/png_3" 111 android:paddingBottom="10dp" 112 android:paddingLeft="20dp" 113 android:paddingRight="20dp" 114 android:paddingTop="10dp" 115 android:text="加入购物车" 116 android:textColor="@android:color/white" /> 117 118 <Button 119 android:id="@+id/doCart" 120 android:layout_width="wrap_content" 121 android:layout_height="wrap_content" 122 android:layout_weight="1" 123 android:background="@drawable/png_3" 124 android:paddingBottom="10dp" 125 android:paddingLeft="20dp" 126 android:paddingRight="20dp" 127 android:paddingTop="10dp" 128 android:text="立即购买" 129 android:textColor="@android:color/white" /> 130 </LinearLayout> 131 132 </LinearLayout>
另外一种常见的方式 当点击一个下拉箭头 会出现的一个popowind 如下拉选择
下拉选择
1 假如listview的item中有Button,ImageButton,CheckBox等会强制获取焦点的view,这时listview的item无法获取焦点,从而无法被点击
解决方法 在item的根布局增加以下的属性android:descendantFocusability="blocksDescendants"设置后,Button获取焦点,item中的其他控件页可以获取焦点了
如下是主要代码
1 package com.ithello.xiala; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.app.Activity; 7 import android.graphics.drawable.BitmapDrawable; 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.view.ViewGroup; 13 import android.widget.AdapterView; 14 import android.widget.AdapterView.OnItemClickListener; 15 import android.widget.BaseAdapter; 16 import android.widget.EditText; 17 import android.widget.ImageView; 18 import android.widget.ListView; 19 import android.widget.PopupWindow; 20 import android.widget.TextView; 21 22 /** 23 * 实现下拉选择的控件 24 * 25 * @author Administrator 26 * 27 */ 28 public class MainActivity extends Activity implements OnClickListener { 29 /**文本编辑框*/ 30 private EditText editText; 31 /**下拉的图标点击可以出现弹窗*/ 32 private ImageView imageView; 33 /**弹窗中的list数据*/ 34 private List<String> list = new ArrayList<String>(); 35 /***/ 36 private ListView listView; 37 /**弹窗PopupWindow*/ 38 private PopupWindow popupWindow; 39 /**弹窗PopupWindow的默认高度*/ 40 private int popupWindowHight = 300; 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 // TODO Auto-generated method stub 45 super.onCreate(savedInstanceState); 46 setContentView(R.layout.mainactivity); 47 setView(); 48 setData(); 49 } 50 51 private void setView() { 52 // TODO Auto-generated method stub 53 editText = (EditText) findViewById(R.id.edt_main); 54 imageView = (ImageView) findViewById(R.id.iv); 55 56 imageView.setOnClickListener(this); 57 } 58 59 private void setData() { 60 // TODO Auto-generated method stub 61 // 初始化数据 62 for (int i = 0; i < 15; i++) { 63 list.add(900000 + i + ""); 64 } 65 66 listView = new ListView(this); 67 listView.setVerticalScrollBarEnabled(false);// 隐藏listview的滚动条
listView.setBackgroundColor(Color.parseColor("#E6E6E6"));
listView.setDivider(new ColorDrawable(Color.WHITE));
listView.setDividerHeight(1); 68 MyAdapter adapter = new MyAdapter(); 69 listView.setAdapter(adapter); 70 71 listView.setOnItemClickListener(new OnItemClickListener() { 72 73 @Override 74 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 75 long arg3) { 76 // TODO Auto-generated method stub 77 Log.d("jiejie", arg2 + ""); 78 editText.setText(list.get(arg2)); 79 popupWindow.dismiss(); 80 } 81 }); 82 } 83 84 /** 85 * 显示一个PopupWindow 86 */ 87 private void showNumPopu() { 88 if (popupWindow == null) { 89 popupWindow = new PopupWindow(listView, editText.getWidth(), 90 popupWindowHight); 91 } 92 // 要让其中的view获取焦点,必须设置为true 93 popupWindow.setFocusable(true); 94 // 还必须设置一个背景图片 95 popupWindow.setBackgroundDrawable(new BitmapDrawable()); 96 // 设置点击外部点击消失 97 popupWindow.setOutsideTouchable(true); 98 popupWindow.showAsDropDown(editText, 0, 0); 99 } 100 101 @Override 102 public void onClick(View arg0) { 103 // TODO Auto-generated method stub 104 switch (arg0.getId()) { 105 case R.id.iv: 106 showNumPopu(); 107 break; 108 109 default: 110 break; 111 } 112 } 113 114 // list的适配器 115 private class MyAdapter extends BaseAdapter { 116 117 @Override 118 public int getCount() { 119 // TODO Auto-generated method stub 120 return list.size(); 121 } 122 123 @Override 124 public Object getItem(int arg0) { 125 // TODO Auto-generated method stub 126 return arg0; 127 } 128 129 @Override 130 public long getItemId(int arg0) { 131 // TODO Auto-generated method stub 132 return arg0; 133 } 134 135 @Override 136 public View getView(final int position, View arg1, ViewGroup arg2) { 137 // TODO Auto-generated method stub 138 final View view = View.inflate(MainActivity.this, 139 R.layout.item_list, null); 140 TextView tv_number = (TextView) view.findViewById(R.id.item_text); 141 ImageView iv_delete = (ImageView) view 142 .findViewById(R.id.item_image); 143 tv_number.setText(list.get(position)); 144 iv_delete.setOnClickListener(new OnClickListener() { 145 146 @Override 147 public void onClick(View arg0) { 148 // TODO Auto-generated method stub 149 Log.d("jiejie", "position " + position); 150 list.remove(position); 151 notifyDataSetChanged(); 152 int listviewHeight = view.getHeight() * list.size(); 153 popupWindow.update( 154 editText.getWidth(), 155 listviewHeight > popupWindowHight ? popupWindowHight 156 : listviewHeight); 157 158 if (list.size() == 0) { 159 imageView.setVisibility(View.INVISIBLE); 160 popupWindow.dismiss(); 161 } 162 163 } 164 }); 165 return view; 166 } 167 168 } 169 }
<?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>