Scrolling view (ScrollView) is when you have a lot of content, and when the screen is not finished, you need to scroll to display the full view. Includes horizontal scrolling view (Horizontalscrollview) and vertical scrolling view (ScrollView)
Basic operation:
Use of Setontouchlistener: Determine when ScrollView slide to the bottom
1, getscorolly ()--the distance of scroll bar sliding
2, Getmeasuredheight ()--the overall height of the content, including hidden parts
3, GetHeight ()--display height. Content is not full of screen, 2=3; content is larger than screen, 3 = screen height, 2>3.
4, Getchildat (int i)--Get Scorollview I child control
ScrollTo (relative start position) and Scrollby (relative to previous position): Control the location of the ScrollView view
Hide scroll bar
1. Label attribute: android:scrollbars= "None"
2. Code settings:
Sethorizontalscrollbarenabled (false);//Hide Transverse Scorollview
Setverticalscrollbarenabled (false);//Hide Portrait Scorollview
Note: The ScrollView content has only one child view.
1.ScrollView allows content to scroll.
Layout:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"> <HorizontalscrollviewAndroid:id= "@+id/horizontalscrollview1"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:scrollbars= "None" > <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"android:textsize= "22SP"Android:text= "Shuiping scroll dfffffffffffffffffffffffffffffffffffffffffffffffffffffff"/> </Horizontalscrollview> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Up" /> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Down" /> <ScrollViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/scrollview"Android:scrollbars= "vertical"android:scrollbartrackvertical= "@color/coloraccent"android:scrollbarthumbvertical= "@color/colorprimary"Android:scrollbarstyle= "Outsideinset"> <LinearLayoutAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "vertical"> <TextViewAndroid:layout_width= "5000DP"Android:layout_height= "500DP"Android:text= "Zcxzcxzcx"/> <TextViewAndroid:layout_width= "5000DP"Android:layout_height= "500DP"Android:text= "Zcxzcxzcx"/> </LinearLayout></ScrollView></LinearLayout>
2. The Code response function is as follows:
public class Scrollviewacti extends Activity implements view.onclicklistener{private ScrollView ScrollView; Private Button button1; Private Button button2; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.scrollview); Button1 = (Button) Findviewbyid (R.id.button1); Button2 = (Button) Findviewbyid (R.id.button2); ScrollView = (ScrollView) Findviewbyid (R.id.scrollview); Button1.setonclicklistener (this); Button2.setonclicklistener (this); Here is the assignment for TextView, content in the R.string.text, test the best content longer, here no longer posted. Textview.settext (Getresources (). getString (R.string.text)); Scrollview.setontouchlistener (New View.ontouchlistener () {@TargetApi (build.version_codes. Honeycomb) @Override Public boolean OnTouch (View V, motionevent event) {//TODO Auto -generated method Stub switch (event.getaction ()){//Finger lift case MotionEvent.ACTION_UP:break; The finger falls case MotionEvent.ACTION_DOWN:break; Finger swipe case Motionevent.action_move:/** * 1, getscorolly ()--the distance of the scroll bar sliding * 2, getmeasuredheight ()--the overall height of the content, including the hidden part * 3, getheight ()--Display height 。 Content is not full of screen, 2=3; content is larger than screen, 3 = screen height, 2>3. *///top state if (scrollview.getscrolly ()<=0) {LOG.E (">>>>>>>>>>>>>> "," top "); Toast.maketext (Getapplicationcontext (), "Top", Toast.length_short). Show (); }//Total height of top state//textview<= height of one screen + scrolling distance of scroll bar (Getchildat(0): No. 0 child control) if (Scrollview.getchildat (0). Getmeasuredheight () <= scrollview.getscrolly ()+ scrollview.getheight ()) {LOG.E (">>>>>>>>>>>>>> "," bottom "); Toast.maketext (Getapplicationcontext (), "bottom", Toast.length_short). Show (); Append content//textview.append ("111111111111111111111") to the text; } break; } return false; } }); } @Override public void OnClick (view v) {//scrollto://scrollby calculated at the beginning of the scrolling view: relative to the previous position, scrolling the corresponding distance Switch (V.getid ()) {case R.id.button1:scrollview.scrollto (0,-30); Scrollview.scrollby (0,-30); Break Case R.id.button2:scrollview.scrollto (0,-30); Scrollview.scrollby (0, 30); Break } }}
3. Effects
android-BASIC Programming-scrollview