直接上代碼 html
activity_popup_window_tooltip_text.xml java
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.artshell.trainingdemos.test.PopupWindowTooltipTextActivity"> <Button android:id="@+id/anchor_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me to see tooltip" android:layout_centerInParent="true"/> </RelativeLayout>
nav_up.xml (drawable) android
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <rotate android:fromDegrees="45" android:pivotX="-50%" android:pivotY="80%" android:toDegrees="45"> <shape android:shape="rectangle"> <solid android:color="#92e4e5"/> <stroke android:width="2dp" android:color="#92e4e5"/> </shape> </rotate> </item> </layer-list>
tooltip_bg.xml (drawable) shell
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#92e4e5"/> <stroke android:width="2dp" android:color="#92e4e5"/> <corners android:radius="2dp"/> </shape>
popup_window_tooltip_layout.xml ide
<?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:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/tooltip_nav_up" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/nav_up"/> <TextView android:id="@+id/tooltip_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/tooltip_bg" android:gravity="center" android:padding="10dp" android:text="Tooltip using PopupWindow :)" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold"/> </LinearLayout>
Activity代碼: this
public class PopupWindowTooltipTextActivity extends ActionBarActivity { private View contentView; private PopupWindow tipWindow; private Button anchor_view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popup_window_tooltip_text); anchor_view = (Button) findViewById(R.id.anchor_view); contentView = getLayoutInflater().inflate(R.layout.popup_window_tooltip_layout, null); tipWindow = new PopupWindow(this); tipWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { tipWindow.dismiss(); } // 這個地方必定要返回false,你可能會說這裏已經消費掉事件了啊應該返回true,參看PopupWindow的私有類PopupViewContainer // 彈出的View都是交由這個類來管理的,由於它對onTouchEvent()這個回調方法已經提供了一些默認實現,當你是返回true,事件就在此被攔截 // 不會再繼續派發了,因此onTouchEvent()沒法被調用 return false; } }); anchor_view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tipWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); tipWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); tipWindow.setOutsideTouchable(true); tipWindow.setTouchable(true); tipWindow.setFocusable(true); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.easyicon_net); // 某些博客說,當沒有設置Background的時候,不管是觸摸其餘空白區仍是back鍵都沒法dismiss()掉這個PopupWindow // 但我註釋掉這段代碼能dismiss(),有多是個Bug,已經得google到修復 tipWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),bitmap)); tipWindow.setContentView(contentView); // 計算這個PopupWindow的顯示位置 int[] screen_pos = new int[2]; anchor_view.getLocationOnScreen(screen_pos); Rect anchor_rect = new Rect(screen_pos[0], screen_pos[1], screen_pos[0] + anchor_view.getWidth(), screen_pos[1] + anchor_view.getHeight()); contentView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); int contentViewWidth = contentView.getMeasuredWidth(); int contentViewHeight = contentView.getMeasuredHeight(); int position_x = anchor_rect.centerX() - (contentViewWidth / 2); int position_y = anchor_rect.bottom - (anchor_rect.height() / 2); tipWindow.showAtLocation(anchor_view, Gravity.NO_GRAVITY, position_x, position_y); } }); } }
還要注意一點就是:當你已經dismiss()掉了,想再次顯示PopupWindow,那麼它的某些屬性須要從新設置,不能一處寫好多處使用,參看這個dismiss()方法的源碼實現。在沒有dismiss()以前,你的PopupWindow內容發生變化了,你能夠使用它的update()系列方法來更新....... google
這裏再帖兩個參考連接,但願對你有所幫助 spa
http://www.cnblogs.com/mengdd/p/3569127.html 【Android PopupWindow的使用和分析】 code
http://michaelye1988.iteye.com/blog/1766629 【Popupwindow的使用】 xml