Many applications in order to save space and the interface can be sufficient to display information, the majority of applications have adopted the way of the sidebar, the following figure:
To say its idea, the bottom is two or more views, respectively, by controlling their width, the left margin to control their display, to see the code
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:id=" @+id/layout "
android:layout_width=" Match_parent "
android: layout_height= "Match_parent"
android:orientation= "horizontal"
tools:context= ". Mainactivity ">
<linearlayout
android:id=" @+id/menu "android:layout_width=" Match_parent "
android:layout_height= "match_parent"
android:background= "@drawable/menu" >
</linearlayout >
<linearlayout
android:id= "@+id/content"
android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:background= "@drawable/content" >
</linearlayout >
</LinearLayout>
Mainactivity.java
public class Mainactivity extends activity implements Ontouchlistener {private LinearLayout menu;
Private LinearLayout content;
Private Layoutparams Menuparams;
Private Layoutparams Contentparams;
The width value of the content when menu is fully displayed.
private static final int menupadding = 80;
resolution private int displaywidth;
private float Xdown;
private float Xmove;
Private Boolean misshow = false;
private static final int speed = 50;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_main);
Displaywidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth ();
menu = (linearlayout) Findviewbyid (R.id.menu);
Content = (linearlayout) Findviewbyid (r.id.content);
Menuparams = (layoutparams) menu.getlayoutparams ();
Contentparams = (layoutparams) content.getlayoutparams (); Findviewbyid (r.id.layout). Setontouchlistener (This);
Menuparams.width = displaywidth-menupadding;
Contentparams.width = Displaywidth;
ShowMenu (misshow); @Override public boolean Ontouch (View v., motionevent event) {switch (event.getaction ()) {case Motio
NEvent.ACTION_DOWN:showMenu (!misshow);
Break
Case MotionEvent.ACTION_MOVE:break;
Case MotionEvent.ACTION_UP:break;
return true;
} private void ShowMenu (Boolean isshow) {if (isshow) {misshow = true;
Menuparams.leftmargin = 0;
else {misshow = false;
Menuparams.leftmargin = 0-menuparams.width;
} menu.setlayoutparams (Menuparams);
}
}
The above code only uses two pictures to replace two complex view (layout), you will find that although two views can be switched, but there is no animation feeling, plus to have a drag effect, so we give it a translation time period, The effect that appears to have animation
Package com.example.test;
Import android.app.Activity;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.View.OnTouchListener;
Import Android.view.Window;
Import Android.widget.LinearLayout;
Import Android.widget.LinearLayout.LayoutParams;
public class Mainactivity extends activity implements Ontouchlistener, Onclicklistener {private LinearLayout menu;
Private LinearLayout content;
Private Layoutparams Menuparams;
Private Layoutparams Contentparams;
The width value of the content when menu is fully displayed.
private static final int menupadding = 80;
resolution private int displaywidth;
private float Xdown;
private float Xmove;
Private Boolean misshow = false;
private static final int speed = 50;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feaTure_no_title);
Setcontentview (R.layout.activity_main);
Displaywidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth ();
menu = (linearlayout) Findviewbyid (R.id.menu);
Menu.setonclicklistener (this);
Content = (linearlayout) Findviewbyid (r.id.content);
Content.setonclicklistener (this);
Menuparams = (layoutparams) menu.getlayoutparams ();
Contentparams = (layoutparams) content.getlayoutparams ();
Findviewbyid (r.id.layout). Setontouchlistener (this);
Menuparams.width = displaywidth-menupadding;
Contentparams.width = Displaywidth;
ShowMenu (misshow); @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.menu:new showmenuasynct
Ask (). Execute (-50);
Break
Case R.id.content:new Showmenuasynctask (). Execute (50);
Break @Override public boolean Ontouch (View v., motionevent event) {switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:shoWmenu (!misshow);
Break
Case MotionEvent.ACTION_MOVE:break;
Case MotionEvent.ACTION_UP:break;
return true;
} private void ShowMenu (Boolean isshow) {if (isshow) {misshow = true;
Menuparams.leftmargin = 0;
else {misshow = false;
Menuparams.leftmargin = 0-menuparams.width;
} menu.setlayoutparams (Menuparams); /** * * This is the main code: simulate the animation process, also let me more familiar with the asynctask this thing * * * */class Showmenuasynctask extends Asynctask<integer, intege R, integer> {@Override protected integer doinbackground (integer ... params) {int leftMargin = Men
Uparams.leftmargin;
Here is also a place to learn, if in the usual, I am sure it is written as follows://if () {//while ()///else if () {/s ()
while (true) {LeftMargin + = params[0];
if (Params[0] > 0 && leftMargin >= 0) {break; else if (Params[0] < 0 && leftMargin <=-menuparams.width) {break;
} publishprogress (LeftMargin);
try {thread.sleep (30);
catch (Interruptedexception e) {e.printstacktrace ();
} return leftMargin;
} @Override protected void Onprogressupdate (Integer ... values) {super.onprogressupdate (values);
Menuparams.leftmargin = Values[0];
Menu.setlayoutparams (Menuparams);
@Override protected void OnPostExecute (Integer result) {Super.onpostexecute (result);
Menuparams.leftmargin = result;
Menu.setlayoutparams (Menuparams);
}
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.