The project will always encounter such an egg pain, Android and iOS the same function, all with the same control, do the effect is: Android Ugly No, iOS is pretty. So, the boss a pat on the butt, the android changed to the same as the iOS style is good. Then, Android development on the bitter, a lot more work out of thin air. Well, don't say much, go directly to the code. Here's a task for an imitation iOS selector I've encountered on my project, and I'll stick to the code side and say it.
First, look at the layout file for this selector.
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:gravity=" Center_horizont Al "android:orientation=" vertical "> <linearlayout android:id=" @+id/pop_layout "Android:layout_w Idth= "Fill_parent" android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true" Andro Id:background= "#ffffff" android:gravity= "center_horizontal" android:orientation= "vertical" > <r Elativelayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" > <button android:id= "@+id/cancle" android:layout_width= "Wrap_content" and roid:layout_height= "Wrap_content" android:background= "@null" android:text= "@string/cancel" Android:textcolor= "#999999 "/> <button android:id=" @+id/ok "android:layout_width=" wrap_content "Android:layout_height=" Wrap_content "android:layout_alignparentright=" true " Android:background= "@null" android:text= "@string/ok" android:textcolor= "@color/textlightgre En "/> </RelativeLayout> <textview android:layout_width=" Fill_parent "Android oid:layout_height= "1DP" android:background= "#999999"/> <scrollview Android:layout_widt H= "Fill_parent" android:layout_height= "120DP" > <radiogroup android:id= "@+id/un Til "android:layout_width=" fill_parent "android:layout_height=" Match_parent " android:orientation= "vertical" > </RadioGroup> </ScrollView> </LinearLayout>< /relativelayout>
Can be seen, this is to be composed of two buttons and a radiogroup, the radiogroup above a layer of ScrollView, the ScrollView is fixed height, it is not difficult to see, The selector selection is implemented by the RadioButton in Radiogroup, and RadioButton is dynamically loaded in the code.
Here's a look at my code for our custom Popupwindow
public class Unitpopupwindow extends Popupwindow {private Button btn_cancle, btn_ok;private radiogroup unit;private View m Menuview;private Context context;private RadioButton button;private colorstatelist csl;private Boolean statusflag = True , @SuppressLint ("Resourceascolor") public Unitpopupwindow (Activity context, Onclicklistener Itemsonclick, Oncheckedchangelistener checkedchangelistener,arraylist<string> dataList) {super (context); This.context = Context Layoutinflater Inflater = (layoutinflater) context.getsystemservice (context.layout_inflater_service); MMenuView = Inflater.inflate (r.layout.alert_dialogs, null); btn_cancle = (Button) Mmenuview.findviewbyid (r.id.cancle); BTN_OK = ( Button) Mmenuview.findviewbyid (r.id.ok), Unit = (radiogroup) Mmenuview.findviewbyid (R.id.until); for (int i = 0; i < dat Alist.size (); i++) {button = new RadioButton (context); button.setbuttondrawable (null); Button.settext (Datalist.get (i)); Button.setid (i); button.settextsize (+); Button.setgravity (Gravity.centeR); Button.setheight (+); Button.setbuttondrawable (Android. r.color.transparent); button.setpadding (0, 0, 0, 0); CSL = Context.getresources (). Getcolorstatelist ( R.drawable.utilcolor); Button.settextcolor (CSL); WindowManager wm1 = Context.getwindowmanager (); Button.setwidth (Wm1.getdefaultdisplay (). getwidth ()); Button.setfocusable (True); Unit.addview (button);} Cancel button Btn_cancle.setonclicklistener (Itemsonclick);//Set button to listen Unit.setoncheckedchangelistener ( Checkedchangelistener); Btn_ok.setonclicklistener (Itemsonclick);// Set Selectpicpopupwindow Viewthis.setcontentview (Mmenuview);//Set Selectpicpopupwindow pop-up form wide this.setwidth ( layoutparams.match_parent);//Set Selectpicpopupwindow pop-up form for high this.setheight (layoutparams.match_parent);// Set Selectpicpopupwindow pop-up form to click This.setfocusable (TRUE);//Set Selectpicpopupwindow pop-up form animation effect This.setanimationstyle ( R.style.animbottom); Colordrawable DW = new colordrawable (0x00000000);//Set the background this.setbackgrounddrawable (DW) of the Selectpicpopupwindow popup form;// Mmenuview Add Ontouchlistener Monitor to get the touch screen position if outside the selection boxPolygon destroys the popup mmenuview.setontouchlistener (new Ontouchlistener () {public boolean OnTouch (View V, motionevent event) {int Height = Mmenuview.findviewbyid (r.id.pop_layout). GetTop (); int y = (int) event.gety (); if (event.getaction () = = MOTIONEVENT.ACTION_UP) {if (Y < height) {Backgroundalpha (1f);d Ismiss ();}} return true;});} Public Radiogroup Getunit () {return unit;} public void SetStatus (Boolean status) {Statusflag = Status;unit.postinvalidate ();} private void Backgroundalpha (float bgalpha) {windowmanager.layoutparams LP = (Activity) context). GetWindow (). GetAttributes (); lp.alpha = Bgalpha; 0.0-1.0 (Activity) context). GetWindow (). SetAttributes (LP);}}
Notice the two listening events inside.
Onclicklistener: This is a listening event that listens to confirm and cancel two buttons
Oncheckedchangelistener: This is a listening event that listens for RadioButton selected state
By the way, this selector pops up the animation effect code, the above code has set up such properties, to find themselves.
<style name= "Animbottom" parent= "@android: Style/animation" > <item name= "android:windowenteranimation "> @anim/push_bottom_in</item> <item name=" android:windowexitanimation "> @anim/push_bottom_out </item> </style>
<?xml version= "1.0" encoding= "Utf-8"?><!--up and down slide-in--><set xmlns:android= "http://schemas.android.com/ Apk/res/android "> <translate android:duration=" " android:fromydelta=" 100%p " android: Toydelta= "0"/> <alpha android:duration= "" android:fromalpha= "0.0" android:toalpha= " 1.0 "/></set>
<?xml version= "1.0" encoding= "Utf-8"?><!--up and down slide-in--><set xmlns:android= "http://schemas.android.com/ Apk/res/android "> <translate android:duration=" " android:fromydelta=" 0 " android: Toydelta= "50%p"/> <alpha android:duration= "+" android:fromalpha= "1.0" android: Toalpha= "0.0"/></set>
And finally to our call. Suppose a user clicks on a button and a toggle selector pops up, which can be handled in the OnClick event of the button, directly on the code
<pre name= "code" class= "java" > ArrayList mdatalist = new arraylist<string> (); Increase the selection of item Mdatalist.add ("km"); Mdatalist.add ("Mile"); Menuwindow = new Unitpopupwindo W (this, itemsonclick,checkedchangelistener,mdatalist); Menuwindow.setondismisslistener (New Ondismisslistener () {@Overridepublic void Ondismiss () {Backgroundalpha (1f);}}); Menuwindow.showatlocation (This.findviewbyid (r.id.main), gravity.right| Gravity.bottom, 0, 0); Backgroundalpha (0.5f); Private onclicklistener Itemsonclick = new Onclicklistener () { public void OnClick (View v) { Menuwindow.dismiss (); switch (v.getId ()) { Case R.id.ok: //Click to confirm the required actions break; Default: break; } } }; Private Oncheckedchangelistener Checkedchangelistener = new Oncheckedchangelistener () { @Override public void OnCheckedChanged (radiogroup Group, int checkedid) { switch (checkedid) { &NBSp;case 0: mutil = getString (r.string.km_ TXT); mutilstring = "KM"; break; case 1: mutil = getString ( R.string.mile); mutilstring = "Mile"; break; default: break; } } };
The above is the full content of the imitation iOS selector, about Popupwindow some basic API use I don't say much here, not difficult to understand.
Using Popupwindow to make iOS-style selectors