Android Advanced 2 Popupwindow pop-up window (feeling of a little suspension window)

Source: Internet
Author: User

Popupwindow is a pop-up window that can be used to display an arbitrary view, and he needs to rely entirely on layout layouts.

It has no interface and displays the controls in the layout completely in the popup window.

Above two beauty heads is the pop-up window Popupwindow display. is a two button.

Specific implementation:

Note: Those three buttons cannot be obtained by the Findviewbyid () method as with the normal button, the view that must be the first to say the button, view Popview = Layoutinflater.inflate ( R.layout.poplayout, NULL);

My button is in the Poplayout.xml. Finally, it is obtained by Button1 = (Button) Popview.findviewbyid (R.id.button1).

The other thing is: Do not get the button in OnCreate (), but as I do under the OnClick method, and Popupwindow together. This is not necessarily true.

Why would you mention it? Since I got the button in OnCreate (), the button's Click event doesn't work, and I'm not sure. If that Daniel knew, could you tell me.

Showatlocation (Findviewbyid (r.id.edit_layout), gravity.bottom,0, 0);

Set the location of the pop-up window:

The first parameter is the layout of the parent control of the popup window;

The second parameter is the position as left, right, upper, lower, etc.;

The third parameter is the offset in the x direction;

The fourth parameter is the offset in the y direction.

[Java]View Plaincopy
  1. Package Xiaosi.popwindow;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import android.view.Gravity;
  5. Import Android.view.LayoutInflater;
  6. Import Android.view.View;
  7. Import Android.view.View.OnClickListener;
  8. Import Android.view.WindowManager.LayoutParams;
  9. Import Android.widget.Button;
  10. Import Android.widget.PopupWindow;
  11. Import Android.widget.Toast;
  12. Public class Popwindowactivity extends Activity implements Onclicklistener
  13. {
  14. /** Called when the activity is first created. * /
  15. Private button button = null;
  16. private Button button1 = null;
  17. private Button button2 = null;
  18. private Button button3 = null;
  19. @Override
  20. public void OnCreate (Bundle savedinstancestate)
  21. {
  22. super.oncreate (savedinstancestate);
  23. Setcontentview (R.layout.main);
  24. Button = (button) Findviewbyid (R.id.button);
  25. Button.setonclicklistener (this);
  26. }
  27. public void OnClick (View arg0)
  28. {
  29. if (arg0.getid () = = R.id.button)
  30. {
  31. Layoutinflater Layoutinflater = (layoutinflater) (popwindowactivity. This)
  32. . Getsystemservice (Layout_inflater_service);
  33. //Get a view of the custom layout file Poplayout.xml
  34. View Popview = layoutinflater.inflate (r.layout.poplayout, null);
  35. Popupwindow Popwindow = new Popupwindow (Popview,
  36. Layoutparams.wrap_content, layoutparams.wrap_content);
  37. //Specify the location of the pop-up window
  38. Popwindow.showatlocation (Findviewbyid (R.id.main), Gravity.bottom,
  39. 0, 0);
  40. two buttons in the//popupwindow
  41. Button1 = (Button) Popview.findviewbyid (R.id.button1);
  42. Button1.setonclicklistener (this);
  43. Button2 = (Button) Popview.findviewbyid (R.id.button2);
  44. Button2.setonclicklistener (this);
  45. }
  46. Else if (arg0.getid () = = R.id.button1)
  47. {
  48. Toast.maketext (popwindowactivity. This, "Button1", Toast.length_long)
  49. . Show ();
  50. }
  51. Else if (arg0.getid () = = R.id.button2)
  52. {
  53. Toast.maketext (popwindowactivity. This, "Button2", Toast.length_long)
  54. . Show ();
  55. }
  56. }
  57. }



Poplayout.xml

[Java]View Plaincopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. Android:layout_width="Wrap_content"
  4. android:layout_height="Wrap_content"
  5. android:gravity="Center_horizontal"
  6. android:orientation="Horizontal" >
  7. <button
  8. android:id="@+id/button1"
  9. Android:layout_width="Wrap_content"
  10. android:layout_height="Wrap_content"
  11. android:background="@drawable/colorframe_1"/>
  12. <button
  13. android:id="@+id/button2"
  14. Android:layout_width="Wrap_content"
  15. android:layout_height="Wrap_content"
  16. android:background="@drawable/colorframe_3"/>
  17. </LinearLayout>


Main.xml

[Java]View Plaincopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. android:id="@+id/main"
  4. Android:layout_width="Fill_parent"
  5. android:layout_height="Fill_parent"
  6. android:orientation="Vertical"
  7. android:background="@drawable/background" >
  8. <button
  9. android:id="@+id/button"
  10. Android:layout_width="Fill_parent"
  11. android:layout_height="Wrap_content"
  12. android:text="Click to see Effects"/>
  13. <imageview
  14. Android:layout_width="Fill_parent"
  15. android:layout_height="Wrap_content"
  16. android:src="@drawable/a"/>
  17. </LinearLayout>



The above is only used for learning, take out and share with you.

Have the source code to give me a message.

http://blog.csdn.net/sjf0115/article/details/7339914

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.