I created a working example based on this Google Groups post.
To create a simple working PopupWindow, you'll need to do the following:
- Create a layout XML which describes the View that will be rendered within the PopupWindow.
- Invoke the PopupWindow by inflating the layout XML, and assign the appropriate "parent view" to the pop-up.
popup_example.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up"
/>
</LinearLayout>
Java code:
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.popup_example, null, false),
100,
100,
true);
// The code below assumes that the root container has an id called 'main'
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
http://rsequence.com/android_blog/node/160
private DismissPopup mDismissPopup = new DismissPopup();
if (mShowToast) {
LayoutInflater inflater;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mPopupView = inflater.inflate(R.layout.month_bubble, null);
mPopup = new PopupWindow(activity);
mPopup.setContentView(mPopupView);
Resources.Theme dialogTheme = getResources().newTheme();
dialogTheme.applyStyle(android.R.style.Theme_Dialog, true);
TypedArray ta = dialogTheme.obtainStyledAttributes(new int[] {
android.R.attr.windowBackground });
mPopup.setBackgroundDrawable(ta.getDrawable(0));
ta.recycle();
}
if (mShowToast) {
mPopup.dismiss();
mPopup.setWidth(width - 20);
mPopup.setHeight(POPUP_HEIGHT);
}
mPopup.setHeight(popupHeight);
if (mPreviousPopupHeight != popupHeight) {
mPreviousPopupHeight = popupHeight;
mPopup.dismiss();
}
mPopup.showAtLocation(this, Gravity.BOTTOM | Gravity.LEFT, 0, 0);
postDelayed(mDismissPopup, POPUP_DISMISS_DELAY);
class DismissPopup implements Runnable {
public void run() {
mPopup.dismiss();
}
}
// This is called when the activity is paused so that the popup can
// be dismissed.
void dismissPopup() {
if (!mShowToast) {
return;
}
// Protect against null-pointer exceptions
if (mPopup != null) {
mPopup.dismiss();
}
Handler handler = getHandler();
if (handler != null) {
handler.removeCallbacks(mDismissPopup);
}
}
'0.일반개발' 카테고리의 다른 글
Thumb Viewer Image (0) | 2010.09.14 |
---|---|
OpenGL ES 1.0 on Android – Triangle Example (2) | 2010.09.14 |
Android 2.0 변경사항 (0) | 2010.09.14 |
안드로이드 제스쳐 Android Gesture (0) | 2010.09.14 |
화면 속성 : 설정 , 가로 : 세로 회전 onCreate(). onDestroy() : 고정 (0) | 2010.09.14 |