Notice
Recent Posts
Recent Comments
올해는 머신러닝이다.
Android PopupWindow 관련 팁 본문
출처 : http://blog.naver.com/PostView.nhn?blogId=ezmo01&logNo=110093886802
정말 이정도 되는예제 아니면 올리지 말았으면 좋겠다. 쓰레기같은 소스들때문에 허비한 시간이 아깝다.
First, create an XML layout, name it popup_layout.xml and in the res/layout/ folder:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/popup_menu_root" android:background="#FFFFFF" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/popup_menu_button1" android:text="close" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/popup_menu_button2" android:text="ok2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/popup_menu_button3" android:text="ok3" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Here I have created a linear layout and put three buttons inside. So that the popup is visible I have made the background white.
Now to the code. You probably already have a class where you need to create a popup. You can declare it wherever you need. I needed to show the popup window after user clicks a button, so I made it class-level and declared in the beginning:
private PopupWindow pw;
Next in the appropriate onClickListener I create the PopupWindow and show it:
// get the instance of the LayoutInflater LayoutInflater inflater = (LayoutInflater) PopupWindowClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // inflate our view from the corresponding XML file View layout = inflater.inflate(R.layout.popup_menu, (ViewGroup)findViewById(R.id.popup_menu_root)); // create a 100px width and 200px height popup window pw = new PopupWindow(layout, 100, 200, true); // set actions to buttons we have in our popup Button button1 = (Button)layout.findViewById(R.id.popup_menu_button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View vv) { // close the popup pw.dismiss(); } }); Button button2 = (Button)layout.findViewById(R.id.popup_menu_button2); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View vv) { Toast.makeText(PopupWindowClass.this, "Hello", Toast.LENGTH_LONG).show(); } }); Button button3 = (Button)layout.findViewById(R.id.popup_menu_button3); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View vv) { finish(); } }); // finally show the popup in the center of the window pw.showAtLocation(layout, Gravity.CENTER, 0, 0);[출처] Popupwindow 한참 찾았다.|작성자 ezmo01
'Android > Tip&Tech' 카테고리의 다른 글
SQL function(함수) 모음 (2) | 2011.11.23 |
---|---|
Custom Android Button Style and Theme (1) | 2011.11.23 |
[펌]Android Traceview War Story (0) | 2011.11.22 |
android honeycomb 최적화하기 (0) | 2011.11.21 |
Android Drawable 이미지 리스트 목록(추천) (1) | 2011.11.16 |