Layout file xml
side_bar_scollview.xml//Display the View container
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <HorizontalscrollviewAndroid:id= "@+id/myscrollview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <LinearLayoutAndroid:id= "@+id/ll_layout"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > </LinearLayout> </Horizontalscrollview></LinearLayout>
home.xml//the main page displayed
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:background= "@drawable/home_bg"> <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Home" /></LinearLayout>
menu.xml//the Menu page displayed
<?XML version= "1.0" encoding= "Utf-8"?><Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@drawable/menu_bg"> <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Menu" /></Framelayout>
mainactivity.java//Main activities
PackageCom.example.side_bar_scrollview;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewTreeObserver.OnGlobalLayoutListener;ImportAndroid.widget.HorizontalScrollView;Importandroid.widget.LinearLayout; Public classMainactivityextendsActivity {PrivateHorizontalscrollview ScrollView; PrivateLinearLayout view_layout; Private intwidth; Private intheight; PrivateView Home_view; PrivateView Menu_view; Private floatrate=0.4f; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Correlation Interface IDSetcontentview (R.layout.side_bar_scollview); //associated control IDscrollview=(Horizontalscrollview) Findviewbyid (R.id.myscrollview); View_layout=(LinearLayout) Findviewbyid (r.id.ll_layout); //monitoring LayoutsMylayoutlistener (); //Hide scroll barScrollview.sethorizontalscrollbarenabled (false); } /*** Monitor Layout changes * 1.getViewTreeObserver---View Event Viewer * 2.addOnGlobalLayoutListener * When the global layout changes in a view tree or When the visual state of a view in the view tree changes, the interface class of the callback function to be called **/ Private voidMylayoutlistener () {scrollview.getviewtreeobserver (). Addongloballayoutlistener (NewOngloballayoutlistener () {@Override Public voidongloballayout () {//TODO auto-generated Method Stub//Remove the callback function for the global layout that was previously registeredview_layout.getviewtreeobserver (). Removeongloballayoutlistener ( This); //Gets the width and height of the scrollview measured by the last call to measure ()Height =scrollview.getmeasuredheight (); Width=scrollview.getmeasuredwidth (); //parsing the layout of home pages and menushome_view=getlayoutinflater (). Inflate (R.layout.home,NULL); Menu_view=getlayoutinflater (). Inflate (R.layout.menu,NULL); //add view to View_layoutView_layout.addview (Menu_view, (int) (width*Rate ), height); View_layout.addview (Home_view, width, height); } }); }}
:
Use of the "Android UI" Slide bar