This blog mainly explains how to use Horizontalscrol LView to achieve the effect of left and right sliding.
Horizontalscrollview is actually a framelayout, typically by placing only one linearlayout child control. If you want to add additional controls, use the LinearLayout child control to add additional controls and finally enrich the effect of their content. Where the orientation layout of the LinearLayout setting is horizontal. horizontalscrollview cannot be and ListView listview Has its own scroll bar settings. Similar to TextView.
follows a practical example of Horizontalscrollview:
1. Effect:
2. Implementing the Code
Requirements Analysis: The main implementation of a parent LinearLayout contains code created by the 10 sub-linearlayout, and each child linearlayout add a ImageView and TextView, the part of the implementation of the left and right sliding
Activity_main.xml
<span style= "Font-family:microsoft yahei;font-size:18px;" ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" >
Mainactivity.javaPackage Com.example.horizontalscrollviewdemo;import Android.app.activity;import Android.os.bundle;import Android.view.gravity;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.viewgroup.layoutparams;import Android.widget.horizontalscrollview;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Activity {private Horizontalscrollview scrollview;private linearlayout linear; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ScrollView = ( Horizontalscrollview) This.findviewbyid (r.id.scroll_view); linear = (LinearLayout) This.findviewbyid (R.id.linear); Createchildlinearlayout ();} private void Createchildlinearlayout () {for (int i = 0; i < i++) {Linearlayout.layoutparams LINEARLP = new Linearla Yout. Layoutparams (LAyoutparams.wrap_content, layoutparams.wrap_content); LinearLayout mylinear = new LinearLayout (this); Linearlp.setmargins (5, 0, 5,); Mylinear.setorientation ( linearlayout.vertical); Mylinear.settag (i); Linear.addview (Mylinear, LINEARLP); Linearlayout.layoutparams LP = new Linearlayout.layoutparams (layoutparams.wrap_content, LayoutParams.WRAP_CONTENT) ; ImageView ImageView = new ImageView (this); Imageview.setbackgroundresource (r.drawable.img); Mylinear.addview ( ImageView, LP); Linearlayout.layoutparams TEXTVIEWLP = new Linearlayout.layoutparams (layoutparams.match_parent, LayoutParams.WRAP_ CONTENT); TextView TextView = new TextView (this), Textview.settext (i + ""); textview.setgravity (gravity.center); Mylinear.addview (TextView, TEXTVIEWLP); Mylinear.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {// TODO auto-generated Method Stubtoast.maketext (Mainactivity.this, V.gettag (). toString (), Toast.length_short). Show ();}});}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu){//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); Retu RN true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}
The above is what this blog said.