xxxx.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF">
<ImageView android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:textSize="35px" />
<TextView android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:textSize="14px" />
</LinearLayout>
</LinearLayout>
MainPage.java
public class MainPage extends Activity {
final private String LOG_TAG = "fpMainPage";
private ListView listView;
private LinearLayout layout;
BroadcastReceiverHelper rhelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
listView = new ListView(this);
listView.setPadding(100, 130, 200, 130);
SimpleAdapter adapter = new SimpleAdapter(this, getData(),R.layout.xxxx,
new String[]{"title","info","img"},
new int[]{R.id.title, R.id.info, R.id.img});
listView.setAdapter(adapter);
layout.addView(listView);
setContentView(layout);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.bg5);
layout.setBackgroundColor(0xFFFFFFFF);
listView.setBackgroundDrawable(drawable);
rhelper=new BroadcastReceiverHelper(this);
rhelper.registerAction("android.intent.action.menulongpress");
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "g1");
map.put("info", "google1");
map.put("img", R.drawable.ic_launcher_home);
list.add(map);
return list;
}
public class BroadcastReceiverHelper extends BroadcastReceiver {
Context ct=null;
BroadcastReceiverHelper receiver;
public BroadcastReceiverHelper(Context c){
ct=c;
receiver=this;
}
public void registerAction(String action){
IntentFilter filter=new IntentFilter();
filter.addAction(action);
ct.registerReceiver(receiver, filter);
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.menulongpress")){
Log.d(LOG_TAG, "android.intent.action.menulongpress received!");
showFpPopWindow();
}
}
}
private void showFpPopWindow(){
LinearLayout layout2;
layout2 = new LinearLayout(this);
Button b = new Button(this);
Button b2 = new Button(this);
layout2.addView(b);
layout2.addView(b2);
layout2.setBackgroundDrawable(null);
b.setText("test pop.");
b2.setText("hello!");
PopupWindow pw = new PopupWindow(this);
pw.setContentView(layout2);
pw.setFocusable(true);
pw.setWidth(600);
pw.setHeight(100);
pw.showAtLocation(getWindow().peekDecorView(), Gravity.LEFT | Gravity.BOTTOM, 0, 0);
}
}
解析:长按menu后, PhoneWindow发送Broadcast: android.intent.action.menulongpress,接收到后,弹出PopupWindow。显示效果如下: