标签:popupmenu popupwindow 下拉菜单 android
1、popupwindow的布局文件:menu_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:background="@drawable/rectangle"
android:divider="#d8d8d8"
android:dividerHeight="1px" >
</ListView>
</RelativeLayout>
2、菜单项的布局文件:menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="10.5sp"
android:text="dsss"/>
</RelativeLayout>
3、popupwindow的背景设置:rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 定义使用渐变色填充 -->
<!-- <gradient
android:angle="270"
android:endColor="#000000"
android:startColor="#000000" /> -->
<!-- 定义几何形状的内边距 -->
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
<!-- 定义几何形状的大小 -->
<size
android:height="60dp"
android:width="30dp" />
<!-- 定义使用单种颜色填充 -->
<solid
android:color="#000000"/>
<!-- 绘制边框 -->
<stroke
android:width="1px"
android:color="#f4b00f"/>
</shape>
4、Activity
package com.example.httptest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
public class PopupWindowActivity extends Activity {
private PopupWindow popupWindow;
private View parent;
/** 菜单弹出来时候的菜单项文字 */
private String[] names = { "编辑相册", "删除相册","删除相片" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bn = (Button) findViewById(R.id.bn);
/** PopupWindow的界面 */
View contentView = getLayoutInflater().inflate(R.layout.menu_layout, null);
/** 列表布局界面 */
ListView listView = (ListView) contentView.findViewById(R.id.listView);
/** 设置网格布局的适配器 */
listView.setAdapter(getAdapter());
/** 设置网格布局的菜单项点击时候的Listener */
listView.setOnItemClickListener(new ItemClickListener());
/** 初始化PopupWindow */
popupWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//popupWindow.
popupWindow.setFocusable(true);// 取得焦点
popupWindow.setBackgroundDrawable(new BitmapDrawable());
/** 设置PopupWindow弹出和退出时候的动画效果 */
//popupWindow.setAnimationStyle(R.style.animation);
parent = this.findViewById(R.id.main);
}
private final class ItemClickListener implements OnItemClickListener {
@Override
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 < names.length; i++) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("name", names[i]);
data.add(item);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
R.layout.menu_item, new String[] { "name" },
new int[] { R.id.textView });
return simpleAdapter;
}
public void openPopWindow(View v) {
/** 设置PopupWindow弹出后的位置 */
//popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
int[] location = new int[2];
v.getLocationOnScreen(location);
Log.v("li","按钮的位置:"+location[0]+","+location[1]);
//popupWindow.showAsDropDown(v);
int w = location[0]-10;
//popupWindow.getHeight()
int h = location[1]+70;
Log.v("li","popupwindow的位置:"+w+","+h);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] + 25, location[1] + 67);
}
}
5、效果图
使用popupwindow设计出popupmenu效果的菜单
标签:popupmenu popupwindow 下拉菜单 android
原文地址:http://blog.csdn.net/u012230055/article/details/44083005