1. 效果圖 java
2. 實現代碼android
MainActivityapp
package com.michael.popupwindowdemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageButton; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { private ImageButton ibOperationMore; List<Map<String, String>> moreList; private PopupWindow pwMyPopWindow; private ListView lvPopupList; private int NUM_OF_VISIBLE_LIST_ROWS = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initPopupWindow(); ibOperationMore = (ImageButton) findViewById(R.id.ib_operate_more); ibOperationMore.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (pwMyPopWindow.isShowing()) { pwMyPopWindow.dismiss(); } else { pwMyPopWindow.showAsDropDown(ibOperationMore); } } }); } private void initPopupWindow() { // TODO Auto-generated method stub LayoutInflater inflater = (LayoutInflater) this .getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.task_detail_popupwindow, null); lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list); pwMyPopWindow = new PopupWindow(layout); pwMyPopWindow.setFocusable(true); // 設置opoupWindow 能夠獲取焦點 lvPopupList.setAdapter(new SimpleAdapter(MainActivity.this, moreList, R.layout.list_item_popupwindow, new String[] { "share_key" }, new int[] { R.id.tv_list_item })); lvPopupList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, moreList.get(position).get("share_key"), Toast.LENGTH_LONG).show(); } }); lvPopupList.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth()); pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() + 20) * NUM_OF_VISIBLE_LIST_ROWS); pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable( R.drawable.bg_popupwindow)); pwMyPopWindow.setOutsideTouchable(true); } private void initData() { // TODO Auto-generated method stub moreList = new ArrayList<Map<String, String>>(); Map<String, String> map; map = new HashMap<String, String>(); map.put("share_key", "111"); moreList.add(map); map = new HashMap<String, String>(); map.put("share_key", "222"); moreList.add(map); map = new HashMap<String, String>(); map.put("share_key", "333"); moreList.add(map); } }
activity_main.xmlide
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" tools:context=".MainActivity" > <LinearLayout android:id="@+id/ll_head_bar" style="@style/header_linear_layout" android:layout_alignParentTop="true" > <TextView style="@style/header_text_view" android:visibility="invisible" /> <ImageButton android:id="@+id/ib_operate_more" style="@style/header_button_operate" android:layout_marginLeft="8dip" android:layout_marginRight="10dip" android:src="@drawable/ico_headbar_more" /> </LinearLayout> </RelativeLayout>
task_detail_popupwindow.xmlthis
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ListView android:id="@+id/lv_popup_list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:divider="@drawable/ico_list_item_line_popupwindow" android:focusableInTouchMode="true" android:listSelector="#00000000" /> </LinearLayout>
list_item_popupwindow.xmlspa
<?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:layout_gravity="center" android:orientation="vertical" > <TextView android:id="@+id/tv_list_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:minHeight="40dp" android:minWidth="120dp" android:textColor="@color/popupwindow_list_item_text_selector" android:textSize="20sp" /> </LinearLayout>
btn_back_selector.xmlcode
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_back_selected" android:state_pressed="true"></item> <item android:drawable="@drawable/btn_back_selected" android:state_selected="true"></item> <item android:drawable="@drawable/btn_back_selected" android:state_focused="true"></item> <item android:drawable="@drawable/btn_back"></item> </selector>
btn_operation_selector.xmlxml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_operation_sel" android:state_pressed="true"></item> <item android:drawable="@drawable/btn_operation_sel" android:state_selected="true"></item> <item android:drawable="@drawable/btn_operation_sel" android:state_focused="true"></item> <item android:drawable="@drawable/btn_operation_nor"></item> </selector>
圖片資源 blog