The principle is the combination of WindowManager Dialog and animation.
Here's a very classic, practical and easy-to-use third-party UI Control library for Android: Bottomview (This is also used by Xiaomi's rice UI)
Implementation features:
You can customize the layout in the view that pops up at the bottom;
You can customize whether you can touch the outside to disappear;
Custom events can be customized;
You can customize whether the peripheral background is transparent or not;
You can customize the animation;
If necessary, you can force the display of the top view
Usage:
1. Download the Bottomview.jar library file and put it in the Libs of Android project
2, set the Bottomview theme:
Copy and paste the 2 theme into your project's Res/values/styles.xml
- <!--translucent background theme-->
- <style name= "Bottomviewtheme_defalut" >
- <item name= "Android:windowframe" > @null </item>
- <item name= "Android:windowcontentoverlay" > @null </item>
- <item name= "Android:windowisfloating" >true</item>
- <item name= "Android:windowistranslucent" >false</item>
- <item name= "Android:windownotitle" >true</item>
- <item name= "Android:windowbackground" > @color/white</item>
- <item name= "android:backgrounddimenabled" >true</item>
- <item name= "Android:windowfullscreen" >true</item>
- </style>
- <!--Transparent Background theme-->
- <style name= "Bottomviewtheme_transparent" >
- <item name= "Android:windowframe" > @null </item>
- <item name= "Android:windowisfloating" >true</item>
- <!--Transparent--
- <item name= "Android:windowistranslucent" >false</item>
- <item name= "Android:windowcontentoverlay" > @null </item>
- <item name= "Android:windownotitle" >true</item>
- <item name= "Android:windowbackground" > @color/white</item>
- <item name= "android:backgrounddimenabled" >false</item>
- </style>
Copy Code
also if prompted
- <item name= "Android:windowbackground" > @color/white</item>
Copy Code
If White is not found here, your project Res/values/color.xml no new or white color value, just add it in Res/values/color.xml
- <color name= "White" > #ffffff </color>
Copy Code
This white value can be.
In addition, the view animation theme optional, the proposal also copied in, the effect is better, the code is as follows:
- <font color= "#333333" ><font face= "Arial" > <style name= "Bottomtotopanim" parent= "Android:animation" >
- <item name= "@android: Windowenteranimation" > @anim/bottomview_anim_enter</item>
- <item name= "@android: Windowexitanimation" > @anim/bottomview_anim_exit</item>
- </style></font></font>
Copy CodeRes/anim/bottomview_anim_enter.xml
- <?xml version= "1.0" encoding= "Utf-8"?>
- <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
- <translate
- android:duration= "500"
- Android:fromydelta= "100%p"/>
- </set>
Copy Code
Res/anim/bottomview_anim_exit.xml
- <?xml version= "1.0" encoding= "Utf-8"?>
- <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
- <translate
- android:duration= "500"
- Android:toydelta= "100%p"/>
- </set>
Copy Code
the whole is:
2, some core use code:
- Bottomview Bottomview = new Bottomview (This,
- R.style.bottomviewtheme_defalut, R.layout.bottom_view);
- Bottomview.setanimation (R.style.bottomtotopanim);//Set animation, optional
- Bottomview.showbottomview (FALSE);
Copy Code
If you want to get this view, call the. GetView () method.
One: (Free to play)
The following is the source code:
Package Com.tandong.bottomview;import Java.util.arraylist;import Android.app.activity;import android.os.Bundle; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.button;import Android.widget.ListView; Import Com.tandong.bottomview.adapter.bvadapter;import com.tandong.bottomview.view.bottomview;/** * Bottomview * * www.aplesson.com * * @author Tandong * */public class Mainactivity extends Activity implements Onclicklistener {private Button btn_show;private ListView lv_menu_list;private arraylist<string> menus;private bottomview bottomView;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initview (); InitData ();} private void InitData () {menus = new arraylist<string> (); Menus.add (Getresources (). getString (r.string.menu_ Search); Menus.add (Getresources (). getString (r.string.Menu_filemanage)); Menus.add (Getresources (). getString (R.string.menu_downloadmanage)); Menus.add (Getresources (). GetString (r.string.menu_setting)); Menus.add (Getresources (). getString (R.string.menu_about));} private void Initview () {btn_show = (Button) This.findviewbyid (r.id.btn_show); Btn_show.setonclicklistener (this);} @Overridepublic void OnClick (View arg0) {switch (Arg0.getid ()) {Case R.id.btn_show:bottomview = new Bottomview ( Mainactivity.this,r.style.bottomviewtheme_defalut, R.layout.bottom_view); Bottomview.setanimation ( R.style.bottomtotopanim); Bottomview.showbottomview (false); lv_menu_list = (ListView) Bottomview.getview (). Findviewbyid (r.id.lv_list); Bvadapter adapter = new Bvadapter (mainactivity.this, menus); Lv_menu_list.setadapter (adapter); lv_menu_ List.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> arg0, View arg1,int arg2, long Arg3) {Bottomview.dismissbottomview ();}}); Break;default:break;}}}
Package Com.tandong.bottomview.adapter;import Java.util.arraylist;import Android.content.context;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.TextView; Import com.tandong.bottomview.r;/** * Bottomview * * @author Tandong * */public class Bvadapter extends Baseadapter {pri Vate context c;private arraylist<string> alss;public bvadapter (context context, arraylist<string> als) { THIS.C = CONTEXT;THIS.ALSS = als;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn alss.size ();} @Overridepublic Object getItem (int arg0) {//TODO auto-generated method Stubreturn Alss.get (arg0);} @Overridepublic long getitemid (int arg0) {//TODO auto-generated method Stubreturn arg0;} @Overridepublic view GetView (int position, view Convertview, ViewGroup arg2) {Convertview = View.inflate (c, R.layout.item , null); TextView TV = (TextView) Convertview.findviewbyid (r.id.tv_name); Tv.settext (Alss.get (position)); return converTView;}}
Principle:
Package Com.tandong.bottomview.view;import Android.app.dialog;import Android.content.context;import Android.view.display;import Android.view.view;import Android.view.window;import Android.view.WindowManager;import Android.view.windowmanager.layoutparams;public class bottomview{private View Convertview; Private context context; private int theme; Private Dialog BV; private int animationstyle; Private Boolean istop = false; Public Bottomview (Context c, int theme, View convertview) {this.theme = theme; This.context = C; This.convertview = Convertview; } public Bottomview (Context c, int theme, int resource) {this.theme = theme; This.context = C; This.convertview = View.inflate (c, resource, NULL); } public void Showbottomview (Boolean canceledontouchoutside) {if (This.theme = = 0) THIS.BV = new Dialog (This.con Text); else THIS.BV = new Dialog (This.context, this.theme); This.bv.setCanceledOnTouchOutside (Canceledontouchoutside); This.bv.getWindow (). Requestfeature (1); This.bv.setContentView (This.convertview); Window wm = This.bv.getWindow (); WindowManager m = Wm.getwindowmanager (); Display d = m.getdefaultdisplay (); Windowmanager.layoutparams p = wm.getattributes (); P.width = (D.getwidth () * 1); if (this.istop) p.gravity = 48; else p.gravity = 80; if (This.animationstyle! = 0) {wm.setwindowanimations (This.animationstyle); } wm.setattributes (P); This.bv.show (); } public void Settopifnecessary () {this.istop = true; } public void setanimation (int animationstyle) {this.animationstyle = Animationstyle; Public View GetView () {return this.convertview; } public void Dismissbottomview () {if (THIS.BV! = null) This.bv.dismiss (); }}