Popupwindow
[Function]
Popupwindow is a kind of user reminder, and its overhead is smaller than activity.
[Code steps]
1. Define the layout for popupwindow, for example, hello. xml
Java code
- <? XML version =
"1.0"
Encoding =
"UTF-8"
?>
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: Orientation = "horizontal"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: padding = "10dp"
- >
- <Imageview
- Android: Id = "@ + ID/image"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: src = "@ drawable/robot"
/>
- <Linearlayout
- Android: Orientation = "vertical"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: paddingleft = "20dip"
- >
- <Textview
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "hellopop! "
- />
- <Button
- Android: Id = "@ + ID/hellobutton"
- Android: layout_width = "100dip"
- Android: layout_height = "wrap_content"
- Android: text = "OK"
- />
- </Linearlayout>
- </Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: padding = "10dp"> <imageview Android: Id = "@ + ID/image" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/robot"/> <linearlayout Android: Orientation = "vertical" Android: Layout _ Width = "wrap_content" Android: layout_height = "wrap_content" Android: paddingleft = "20dip"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "hellopop! "/> <Button Android: Id =" @ + ID/hellobutton "Android: layout_width =" 100dip "Android: layout_height =" wrap_content "Android: TEXT = "OK"/> </linearlayout>
2. Use layoutinflater to get the view of Hello. xml
Java code
- View =
This
. Getlayoutinflater (). Inflate (R. layout. Hello,
Null
);
View = This. getlayoutinflater (). Inflate (R. layout. Hello, null );
3. Create popupwindow pop and use the above layout File View
Java code
- Pop =
New
Popupwindow (view,
500
,
200
);
Pop = new popupwindow (view, 500,200 );
4. Pop-up popupwindow
* Layout definition file: Main. xml includes a button
Java code
- <? XML version =
"1.0"
Encoding =
"UTF-8"
?>
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: Orientation = "vertical"
- Android: Id = "@ + ID/main"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- >
- <Textview
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "Pop demo! "
- />
- <Button
- Android: Id = "@ + ID/button"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "to pop! "
- />
- </Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: Id = "@ + ID/main" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Pop demo! "/> <Button Android: Id =" @ + ID/button "Android: layout_width =" fill_parent "Android: layout_height =" wrap_content "Android: text =" to pop! "/> </Linearlayout>
* Pop-up: There are two modes: one is the drop-down mode and the other is the specified location.
-Drop-down list:
Java code
- Findviewbyid (R. Id. Button). setonclicklistener (
New
View. onclicklistener (){
- Public
Void
Onclick (view v ){
- // Todo auto-generated method stub
- Pop. showasdropdown (v );
- }
- });
Findviewbyid (R. id. button ). setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// todo auto-generated method stub pop. showasdropdown (v );}});
-Specified location:
Java code
- Findviewbyid (R. Id. Button). setonclicklistener (
New
View. onclicklistener (){
- Public
Void
Onclick (view v ){
- // Todo auto-generated method stub
- Pop. showatlocation (findviewbyid (R. Id. Main), gravity. Center, 20
,
20
);
- }
- });
Findviewbyid (R. id. button ). setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// todo auto-generated method stub pop. showatlocation (findviewbyid (R. id. main), gravity. center, 20, 20 );}});
5. Cancel
Java code
- View. findviewbyid (R. Id. hellobutton). setonclicklistener (
New
View. onclicklistener (){
- Public
Void
Onclick (view v ){
- // Todo auto-generated method stub
- Pop. Dismiss ();
- }
- });
View. findviewbyid (R. id. hellobutton ). setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// todo auto-generated method stub pop. dismiss ();}});
6. Other problems:
* I found that many people have doubts about which item is clicked after the listview is included in the popupwindow. So I tested it and found that it is the same as the general method. There is nothing special about it. share
It is written that because listview is expanded and displayed, it will lead to poor appearance, so take the spinner as an example.
6.1. Define the layout file "Hello. xml" containing the spinner
Java code
- <? XML version =
"1.0"
Encoding =
"UTF-8"
?>
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: Orientation = "vertical"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- >
- <Linearlayout
- Android: Orientation = "horizontal"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- >
- <Imageview
- Android: Id = "@ + ID/image"
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: src = "@ drawable/robot"
/>
- <Textview
- Android: layout_width = "wrap_content"
- Android: layout_height = "wrap_content"
- Android: text = "hellopop! "
- />
- </Linearlayout>
- <Spinner
- Android: Id = "@ + ID/Spinner"
- Android: layout_width = "wrap_content"
- Android: layout_height = "40dip"
/>
- </Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <linearlayout Android: orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <imageview Android: Id = "@ + ID/image" Android: layout_width = "wrap_content" Android: layout_height = "wrap_conten T "Android: src =" @ drawable/robot "/> <textview Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: text =" hellopop! "/> </Linearlayout> <spinner Android: Id =" @ + ID/Spinner "Android: layout_width =" wrap_content "Android: layout_height =" 40dip "/> </linearlayout>
6.2. Get the instance of the spinner: Spinner
Java code
- Spinner = (spinner) view. findviewbyid (R. Id. spinner );
Spinner = (spinner) view. findviewbyid (R. Id. spinner );
6.3. Bind a spinner with specific data. In this example, contact is used as an example.
Java code
- Public
Void
Specifyspinner (){
- Cursor c = getcontentresolver (). Query (people. content_uri,
- Null
,
Null
,
Null
,
Null
);
- Simplecursoradapter adapter = new
Simplecursoradapter (
This
,
- Android. R. layout. simple_list_item_1, C,
- New
String [] {People. name },
- New
Int
[] {Android. R. Id. text1 });
- Adapter. setdropdownviewresource (
- Android. R. layout. simple_spinner_dropdown_item );
- Spinner. setadapter (adapter );
- }
Public void specifyspinner () {cursor c = getcontentresolver (). query (people. content_uri, null, null); simplecursoradapter adapter = new simplecursoradapter (this, android. r. layout. simple_list_item_1, C, new string [] {people. name}, new int [] {android. r. id. text1}); adapter. setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); spinner. setadapter (adapter );}
Don't forget the contact's access permission:
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>
6.4. Obtain specific items:
Java code
- Spinner. setonitemselectedlistener (
New
Onitemselectedlistener (){
- Public
Void
Onitemselected (adapterview <?> Adapter, view V,
- Int
POs,
Long
ID ){
- Updatetitle (POS );
- }
- Public
Void
Onnothingselected (adapterview <?> Arg0 ){
- // Todo auto-generated method stub
- }
- });
Spinner. setonitemselectedlistener (New onitemselectedlistener () {public void onitemselected (adapterview <?> Adapter, view V, int POs, long ID) {updatetitle (POS);} public void onnothingselected (adapterview <?> Arg0) {// todo auto-generated method stub }});
Write updatetitle (INT) to display the position in the title
Public void updatetitle (int I ){
This. settitle ("hellopop:" + I );
}
6.5. emulator run:
Http://www.iteye.com/topic/604462