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
- Package Xiaosi.popwindow;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.view.Gravity;
- Import Android.view.LayoutInflater;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.view.WindowManager.LayoutParams;
- Import Android.widget.Button;
- Import Android.widget.PopupWindow;
- Import Android.widget.Toast;
- Public class Popwindowactivity extends Activity implements Onclicklistener
- {
- /** Called when the activity is first created. * /
- Private button button = null;
- private Button button1 = null;
- private Button button2 = null;
- private Button button3 = null;
- @Override
- public void OnCreate (Bundle savedinstancestate)
- {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Button = (button) Findviewbyid (R.id.button);
- Button.setonclicklistener (this);
- }
- public void OnClick (View arg0)
- {
- if (arg0.getid () = = R.id.button)
- {
- Layoutinflater Layoutinflater = (layoutinflater) (popwindowactivity. This)
- . Getsystemservice (Layout_inflater_service);
- //Get a view of the custom layout file Poplayout.xml
- View Popview = layoutinflater.inflate (r.layout.poplayout, null);
- Popupwindow Popwindow = new Popupwindow (Popview,
- Layoutparams.wrap_content, layoutparams.wrap_content);
- //Specify the location of the pop-up window
- Popwindow.showatlocation (Findviewbyid (R.id.main), Gravity.bottom,
- 0, 0);
- two buttons in the//popupwindow
- Button1 = (Button) Popview.findviewbyid (R.id.button1);
- Button1.setonclicklistener (this);
- Button2 = (Button) Popview.findviewbyid (R.id.button2);
- Button2.setonclicklistener (this);
- }
- Else if (arg0.getid () = = R.id.button1)
- {
- Toast.maketext (popwindowactivity. This, "Button1", Toast.length_long)
- . Show ();
- }
- Else if (arg0.getid () = = R.id.button2)
- {
- Toast.maketext (popwindowactivity. This, "Button2", Toast.length_long)
- . Show ();
- }
- }
- }
Poplayout.xml
[Java]View Plaincopy
- <?xml version="1.0" encoding="Utf-8"?>
- <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:orientation="Horizontal" >
- <button
- android:id="@+id/button1"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:background="@drawable/colorframe_1"/>
- <button
- android:id="@+id/button2"
- Android:layout_width="Wrap_content"
- android:layout_height="Wrap_content"
- android:background="@drawable/colorframe_3"/>
- </LinearLayout>
Main.xml
[Java]View Plaincopy
- <?xml version="1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
- android:id="@+id/main"
- Android:layout_width="Fill_parent"
- android:layout_height="Fill_parent"
- android:orientation="Vertical"
- android:background="@drawable/background" >
- <button
- android:id="@+id/button"
- Android:layout_width="Fill_parent"
- android:layout_height="Wrap_content"
- android:text="Click to see Effects"/>
- <imageview
- Android:layout_width="Fill_parent"
- android:layout_height="Wrap_content"
- android:src="@drawable/a"/>
- </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