1:先看運行效果:
2:直接上代碼:MainActivity.java
</p><p> package com.youpeng.popwindowdemo;</p><p>import android.app.Activity;<br />import android.content.Context;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.view.Gravity;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.view.View.OnClickListener;<br />import android.view.ViewGroup.LayoutParams;<br />import android.widget.Button;<br />import android.widget.LinearLayout;<br />import android.widget.PopupWindow;</p><p>public class MainActivity extends Activity implements OnClickListener {</p><p>private Button button;<br />private static final int FIRST = 1;<br />private static final int SECOND = 2;<br />private PopupWindow window;<br />private View view;<br />private Button btnSms, btnEmail, btnCall, btnChat;</p><p>@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);</p><p>button = (Button) findViewById(R.id.button);<br />button.setOnClickListener(new OnClickListener() {<br />@Override<br />public void onClick(View v) {<br />// 顯示 popupWindow<br />window = CreatePopupWindow(MainActivity.this);<br />// PopupWindow popupWindow = makePopupWindow(MainActivity.this);<br />int[] xy = new int[2];<br />button.getLocationOnScreen(xy);<br />window.showAtLocation(button, Gravity.RIGHT | Gravity.TOP,<br />-xy[0] / 2, xy[1] + button.getWidth());<br />// popupWindow.showAsDropDown(button,0, 0);<br />}<br />});<br />}<br />// 建立一個包含自訂view的PopupWindow,通過xml布局<br />private PopupWindow CreatePopupWindow(MainActivity mainActivity) {</p><p>view = this.getLayoutInflater().inflate(R.layout.dialog2, null);<br />window = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,<br />ViewGroup.LayoutParams.WRAP_CONTENT);</p><p>btnSms = (Button) view.findViewById(R.id.btnSms);<br />btnSms.setOnClickListener(this);<br />btnSms.setBackgroundResource(R.drawable.sms_selector);</p><p>btnCall = (Button) view.findViewById(R.id.btnCall);<br />btnCall.setOnClickListener(this);<br />btnCall.setBackgroundResource(R.drawable.call_selector);</p><p>btnChat = (Button) view.findViewById(R.id.btnChat);<br />btnChat.setOnClickListener(this);<br />btnChat.setBackgroundResource(R.drawable.chat_selector);</p><p>btnEmail = (Button) view.findViewById(R.id.btnEmail);<br />btnEmail.setOnClickListener(this);<br />btnEmail.setBackgroundResource(R.drawable.email_selector);</p><p>// 設定PopupWindow外部地區是否可觸摸<br />window.setFocusable(true); // 設定PopupWindow可獲得焦點<br />window.setTouchable(true); // 設定PopupWindow可觸摸<br />window.setOutsideTouchable(true); // 設定非PopupWindow地區可觸摸<br />view.setOnClickListener(new View.OnClickListener() {<br />@Override<br />public void onClick(View v) {<br />dismissWindow();<br />}<br />});</p><p>return window;<br />}</p><p>// 建立一個包含自訂view的PopupWindow,通過代碼布局<br />private PopupWindow makePopupWindow(Context cx) {</p><p>window = new PopupWindow(cx);<br />Button b1 = new Button(this);<br />b1.setText("first");<br />b1.setId(FIRST);<br />b1.setOnClickListener(this);<br />b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,<br />LayoutParams.WRAP_CONTENT));</p><p>Button b2 = new Button(this);<br />b2.setText("Second");<br />b2.setId(SECOND);<br />b2.setOnClickListener(this);<br />b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,<br />LayoutParams.WRAP_CONTENT));</p><p>LinearLayout linearLayout = new LinearLayout(this);<br />linearLayout.addView(b1);<br />linearLayout.addView(b2);<br />linearLayout.setOrientation(LinearLayout.VERTICAL);<br />window.setContentView(linearLayout);</p><p>window.setBackgroundDrawable(getResources().getDrawable(<br />R.drawable.popwindow_bg));<br />window.setWidth(DisplayManager.dipToPixel(getApplicationContext(), 150));<br />window.setHeight(DisplayManager<br />.dipToPixel(getApplicationContext(), 150));</p><p>// 設定PopupWindow外部地區是否可觸摸<br />window.setFocusable(true); // 設定PopupWindow可獲得焦點<br />window.setTouchable(true); // 設定PopupWindow可觸摸<br />window.setOutsideTouchable(true); // 設定非PopupWindow地區可觸摸<br />return window;<br />}</p><p>@Override<br />public void onClick(View v) {<br />switch (v.getId()) {<br />case R.id.btnSms:<br />Intent intentSms = new Intent(this, SmsActivity.class);<br />startActivity(intentSms);<br />dismissWindow();<br />break;<br />case R.id.btnCall:<br />Intent intentCall = new Intent(this, CallActivity.class);<br />startActivity(intentCall);<br />dismissWindow();<br />break;<br />case R.id.btnChat:<br />Intent intentChat = new Intent(this, ChatActivity.class);<br />startActivity(intentChat);<br />dismissWindow();<br />break;<br />case R.id.btnEmail:<br />Intent intentEmail = new Intent(this, EmailActivity.class);<br />startActivity(intentEmail);<br />dismissWindow();<br />break;<br />default:<br />dismissWindow();<br />break;<br />}<br />}</p><p>private void dismissWindow() {<br />if (window.isShowing())<br />window.dismiss();<br />}<br />}<br />
3:main.xml
<?xml version="1.0" encoding="utf-8"?><br /><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"></p><p> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:color/darker_gray" android:orientation="horizontal"></p><p> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Title"></p><p> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="click"><br /> </button></textview></linearlayout></p><p></linearlayout><br />
4:dialog2.xml
<?xml version="1.0" encoding="utf-8"?><br /><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:background="@drawable/popwindow_bg" android:orientation="vertical"></p><p> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal"></p><p> <button android:id="@+id/btnSms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:background="@drawable/sms_icon"></p><p> </button><button android:id="@+id/btnEmail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:background="@drawable/email_icon"><br /> </button></linearlayout></p><p> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal"></p><p> <button android:id="@+id/btnCall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:background="@drawable/call_icon"></p><p> </button><button android:id="@+id/btnChat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip" android:background="@drawable/chat_icon"><br /> </button></linearlayout></p><p></linearlayout><br />
5:幾個button背景選取器的xml
5.1:call_selector.xml
<?xml version="1.0" encoding="utf-8"?></p><p><selector xmlns:android="http://schemas.android.com/apk/res/android"></p><p> <item android:state_selected="true" android:drawable="@drawable/call_icon_press"></p><p> <item android:state_focused="true" android:drawable="@drawable/call_icon_press"></p><p> <item android:state_pressed="true" android:drawable="@drawable/call_icon_press"></p><p> <item android:drawable="@drawable/call_icon"></p><p></item></item></item></item></selector></p><p>
5.2:chat_selector.xml
<?xml version="1.0" encoding="utf-8"?></p><p><selector xmlns:android="http://schemas.android.com/apk/res/android"></p><p> <item android:state_selected="true" android:drawable="@drawable/chat_icon_press"></p><p> <item android:state_focused="true" android:drawable="@drawable/chat_icon_press"></p><p> <item android:state_pressed="true" android:drawable="@drawable/chat_icon_press"></p><p> <item android:drawable="@drawable/chat_icon"></p><p></item></item></item></item></selector></p><p>
5.3:email_selector.xml
<?xml version="1.0" encoding="utf-8"?></p><p><selector xmlns:android="http://schemas.android.com/apk/res/android"></p><p> <item android:state_selected="true" android:drawable="@drawable/email_icon_press"></p><p> <item android:state_focused="true" android:drawable="@drawable/email_icon_press"></p><p> <item android:state_pressed="true" android:drawable="@drawable/email_icon_press"></p><p> <item android:drawable="@drawable/email_icon"></p><p></item></item></item></item></selector></p><p>
5.4:sms_selector.xml
<?xml version="1.0" encoding="utf-8"?></p><p><selector xmlns:android="http://schemas.android.com/apk/res/android"></p><p> <item android:state_selected="true" android:drawable="@drawable/sms_icon_press"></p><p> <item android:state_focused="true" android:drawable="@drawable/sms_icon_press"></p><p> <item android:state_pressed="true" android:drawable="@drawable/sms_icon_press"></p><p> <item android:drawable="@drawable/sms_icon"></p><p></item></item></item></item></selector></p><p>
6:資源圖片