public class MainActivity extends Activity { //声明PopupWindow对象 PopupWindow popupWindow; View parent; private int[] images = {R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4, R.drawable.i5,R.drawable.i6,R.drawable.i7,R.drawable.i8}; private String[] names = {"搜索", "文件管理", "下载管理", "全屏", "网址", "书签", "加入书签", "分享页面"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通过系统的布局填充器,根据布局文件生成一个view对象 View contentView = getLayoutInflater().inflate(R.layout.popwindow, null); //获取GridView对象 GridView gridView = (GridView) contentView.findViewById(R.id.gridView); //为GridView设置适配器 gridView.setAdapter(getAdapter()); //为GridView中的条目设置点击事件 gridView.setOnItemClickListener(new ItemClickListener()); //实例化PopupWindow对象 popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //设置打开PopupWindow时,PopupWindow获取焦点 popupWindow.setFocusable(true);//取得焦点 //设置PopupWindow在点击别的组件时自动关闭 popupWindow.setBackgroundDrawable(new BitmapDrawable()); //通过样式为PopupWindow设置出现和离开的动画 popupWindow.setAnimationStyle(R.style.animation); parent = this.findViewById(R.id.main); } private final class ItemClickListener implements OnItemClickListener{ //设置点击PopupWindow条目时,PopupWindow自动关闭 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(popupWindow.isShowing()) popupWindow.dismiss();//关闭 //.... } } private ListAdapter getAdapter() { List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>(); for(int i = 0 ; i < images.length ; i++ ){ HashMap<String, Object> item = new HashMap<String, Object>(); item.put("image", images[i]); item.put("name", names[i]); data.add(item); } SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.grid_item, new String[]{"image", "name"}, new int[]{R.id.imageView, R.id.textView}); return simpleAdapter; } public void openPopWindow(View v){ //设置PopupWindow的出场位置 popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0); } }
<?xml version="1.0" encoding="utf-8"?> <resources> <!--配置动画的样式文件--> <style name="animation"> <item name="android:windowEnterAnimation">@anim/enter</item> <item name="android:windowExitAnimation">@anim/out</item> </style> </resources>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <!--进入动画--> <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.7" android:toAlpha="1.0" android:duration="300" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <!--离开的动画--> <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="3000" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.5" android:duration="2000" /> </set>
<?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" android:background="@drawable/bg" > <!--PopupWindow网格布局界面,类似于ListView,需要配合条目界面使用--> <GridView android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="4" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:id="@+id/gridView" /> </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" android:gravity="center" > <!--条目界面-->
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="16sp" android:textColor="#000099" android:id="@+id/textView" /> </LinearLayout>