Popupwindow is a pop-up window that can be used to display any view. It must be completely dependent on layout.
It has no interface, and the layout controls are fully displayed in the pop-up window.
The above two beautiful headers are the content displayed in the pop-up window popupwindow. Is two buttons.
Specific implementation:
Note: The three buttons cannot be obtained through the findviewbyid () method like normal buttons. You must first describe the view where the button is located. View popview = layoutinflater. inflate (R. layout. poplayout, null );
My buttons are in poplayout. xml. Finally, it is obtained through button1 = (button) popview. findviewbyid (R. Id. button1.
Another point is: do not get the button in oncreate (), but get it in The onclick method just like me, with popupwindow. This is not necessarily true.
Why? This is because the button clicking event cannot be used when I obtain the button in oncreate (), and I am not very clear about it. If that Daniel knows, let me know.
Showatlocation (findviewbyid (R. Id. edit_layout), gravity. Bottom, 0, 0 );
Set the position of the pop-up window:
The first parameter is the layout of the pop-up window parent control;
The second parameter is the position such as left, right, top, and bottom;
The third parameter is the offset in the X direction;
The fourth parameter is the offset in the Y direction.
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 = NULL; private button button1 = NULL; private button button2 = NULL; private button button3 = NULL; @ overridepublic 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) (popwindowactivity. this ). getsystemservice (layout_inflater_service); // obtain 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); // specifies the position of the pop-up window. showatlocation (findviewbyid (R. id. main), gravity. bottom, 0, 0); // two buttonbutton1 = (button) popview in the popupwindow. 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
<?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
<? 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 view results"/> <imageview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: src = "@ drawable/a"/> </linearlayout>
The above is just for learning. Let's share it with you.
Leave a message to me if you want the source code.