============ Problem Description ============
The first post, mainly I today in order to achieve a good view of the drop-down list, on the internet to find a day about spinner information, did not see good posts. Try a variety of methods, always find how to set the background of the dropdown box method, if please give me a message, thank you.
Finally, in the great teacher's advice, I decided to change the direction to consider using his popupwindow, so I found a write down the sword, so I can't wait to share this to those who are as lost as I have little friends.
In this case, the Popupwindow can be clicked on the external area to cancel the view function.
<USES-SDK
android:minsdkversion= "14"
android:targetsdkversion= "/> "
Let's see it, it's simple.
Images used:
Here button picture inside of the arrow I do together, if you do not like, want the arrow button to become a button, you can change the main page of the layout file, TextView after adding a button, then set the background is good. Think spinner can do this? I don't want to throw up the groove.
Just fine.
Code: Activity Section
Popwindowspinneractivity.java
Package com.example.spinnertest;import java.util.arraylist;import android.app.activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.listview;import android.widget.popupwindow;import android.widget.textview;/** * This is a popwindow implementation of the drop-down list, compared to spinner, I found spinner, simply useless. * @author lishuai * */public class popwindowspinneractivity extends activity {//drop-down button Textview mybutton;//popupwindow Object declaration popupwindow pw; arraylist<string> list;//the currently selected list item location int clickpsition = -1; @Overridepublic void oncreate (bundle savedinstancestate) {super.oncreate (savedinstancestate); SetContentView ( R.layout.activity_pop_window); mybutton = (TextView) findviewbyid (R.id.mybutton);//Obtain the data to be displayed list = getlist ();// Sets the default display of Textmybutton.settext (list.get (0)); Mybutton.setonclicklistener (new View.onclicklistener () {@Overridepublic void onclick (view v) {//through the layout injector, Inject the layout to the View Object View myview = getlayoutinflater (). Inflate (r.layout.pop, null);//through view and wide · High, tectonic Popopwindowpw = new popupwindow (myview, 240, 300, true); Pw.setbackgrounddrawable (Getresources (). Getdrawable (//here to set the background for popwindow , colleagues do click on the external area, Popwindow disappears R.DRAWABLE.DIAOLOG_BG));//Set the focus to clickable pw.setfocusable (true);//You can try the result set to False// The window view is displayed under MyButton pw.showasdropdown (MyButton); listview lv = (ListView) myview.findviewbyid (R.id.lv_pop); Lv.setadapter (new Listviewadapter (popwindowspinneractivity.this, list)); Lv.setonitemclicklistener (new Adapterview.onitemclicklistener () {@Overridepublic void onitemclick (adapterview<?> Parent, view view,int position, long id) {mybutton.settext (list.get (position)); if (clickpsition != position) {clickpsition&nbsP;= position;} Pw.dismiss ();});}); /** * How to get the list collection * @return */public arraylist<string> getlist () {ArrayList<String> list = new ArrayList<String> (); List.add ("Daily Spit"); List.add ("Inspiration note"), List.add ("Comedy Wang Wen"), List.add ("Connotation Satin"), List.add ("Daily Spit"), List.add ("Inspiration note"), List.add ("Comedy Wang Wen"); List.add ("Connotation Satin"); List.add ("Daily Spit"); List.add ("Inspiration note"), List.add ("Comedy Wang Wen"), List.add ("connotation"); return list;}}
Next is the adapter: This is a small optimization of the next.
Listviewadapter.java
package com.example.spinnertest;import java.util.arraylist;import android.content.context; import android.view.layoutinflater;import android.view.view;import android.view.viewgroup; import android.widget.baseadapter;import android.widget.textview;/** * Adapter * @ author administrator * */public class listviewadapter extends baseadapter{ private layoutinflater inflater;private arraylist<string> list; public Listviewadapter (context context, arraylist<string> list) {super (); this.inflater = layoutinflater.from (context); this.list = list;} @Overridepublic int getcount () {return list.size ();} @Overridepublic object getitem (int position) {// todo auto-generated method stubreturn null;} @Overridepublic long getitemid (int position) &NBSP;{//&NBSP;TODO&NBSp auto-generated method stubreturn 0;} @Overridepublic view getview (int position, view convertview, viewgroup Parent) {if (convertview == null) {convertview = inflater.inflate ( R.layout.lv_items, null);} textview tv = (TextView) Convertview.findviewbyid (R.id.text); Tv.settext (List.get (position)); Return convertview;}}
Next is the Drawable folder inside the Click Effect selector:selector_sinner.xml
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/spinner_bg_press "android:state_pressed=" true "/> <item Android oid:drawable= "@drawable/spinner_bg" android:state_pressed= "false"/></selector>
Then there are several XML files:
File name: Activity_pop_window.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android " android:layout_width=" Fill_parent " android:layout_height= "Fill_parent" android:background= "#fff" android:orientation= "Vertical" > <TextView android:paddingleft= "5DP" android : id= "@+id/mybutton" android:gravity= "center_vertical" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android: Layout_gravity= "Center_horizontal" android:background= "@ Drawable/selector_sinner " &nbSp; android:textcolor= "#000" /></LinearLayout>
File name: Lv_items.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <textview android:layout_marginleft= "20dp" android:id= "@+id/text" android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "android:textcolor=" #000 "/></linearlayout>
File name: Pop.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " android:paddingtop= "60DP" > <listview android:id= "@+id/lv_pop" android:divider= "@color/line" Android:layout_width= "Match_parent" android:layout_height= "Match_parent"/></linearlayout>
After studying Android for a year, I used Popupwindow for the first time today and found it so powerful. Really ashamed ... No more nonsense.
Everybody cheer up.
============ Solution 1============
So professional code, you must be Li Shuai!
============ Solution 2============
The idea of such a bull B must have been taught by the old Bell.
============ Solution 3============
So professional code, you still Li Shuai!
============ Solution 4============
It's so awesome! A look is Drowong Chong!
Farewell spinner drop-down list, with Popupwindow better