Scrolling view of Android view

Source: Internet
Author: User

Scrolling view (ScrollView) is a control that is used when the information that needs to be displayed is not displayed on a screen.

1.ScrollView Overview

The ScrollView is derived from Framelayout and is also located under the Android.widget package. The ScrollView class is actually a frame layout, in which the controls are laid out linearly, and the user can scroll to achieve the purpose of displaying more information on the screen.

By default, ScrollView just adds a vertical scrollbar to other components, and if the app needs to add a horizontal scroll bar, it can be implemented with another scroll bar view. ScrollView is similar to the basic function of Horizontalscrollview, except that the former adds a vertical scrollbar, which adds a horizontal scroll bar.

The use of ScrollView is not very different from the use of normal layouts, it can be configured in an XML file, or it can be set through Java code. You can add any control that satisfies the criteria in ScrollView, and automatically add scrolling functionality when one screen doesn't display all of the controls or information it contains.

Note: The ScrollView can contain only one view at a time.

2. Configuring the ScrollView control in XML

Here's an example to illustrate configuring the ScrollView control in an XML file.

Use the ScrollView class to complete the vertical scrolling of the picture. The steps for its implementation are:

(1) Create an Android app project in Eclipse named Li_scrollviewxml.

(2) Open the Main.xml file in the Res/layout directory, the vertical scrolling of the layout image in the file, the code is:


<?xml version= "1.0"  encoding= "utf-8"?><scrollview xmlns:android =  "http// Schemas.android.com/apk/res/android "    android:layout_width=" Match_parent "     android:layout_height= "match_parent"     android:scrollbars= "vertical"      android:background= "#000000" >    <LinearLayout         android:layout_width= "Match_parent"          android:layout_height= "Match_parent"         android:orientation= "Vertical" >        <ImageView             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:src= "@drawable/study"              Android:layout_gravity= "Center_horizontal"/>        <imageview             android:layout_width= "Wrap_content"             android:layout_height= "Wrap_content"             android:src= "@drawable/study"              android:layout_gravity= "Center_horizontal"/ >        <ImageView             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"               android:src= "@drawable/study"             android: Layout_gravity= "Center_horizontal"/>        <imageview             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:src= "@drawable/study"              android:layout_gravity= "Center_horizontal"/>         <ImageView             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android: src= "@drawable/study"             android:layout_ Gravity= "Center_horizontal"/>        <imageview             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:src= "@drawable/study"              android:layout_gravity= "Center_horizontal"/>         <ImageView             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:src= "@dRawable/study "            android:layout_gravity=" Center_horizontal "/>        <imageview             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:src= "@drawable/study"              android:layout_gravity= "Center_horizontal"/>         <ImageView             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:src= "@drawable/Study "            android:layout_gravity=" Center_ Horizontal "/>        <imageview             android:layout_width= "Wrap_content"              android:layout_height= "Wrap_content"              android:src= "@drawable/study"              android:layout_gravity= "Center_horizontal"/>     </LinearLayout></ScrollView>

3. Configuring the ScrollView control in a Java file


ScrollView Scroll view refers to the use of a scroll bar to display the view when there is a lot of content and the display of a screen is not complete. The following is a case study to demonstrate the configuration of the ScrollView control in a Java file.

The "example" implementation configures the ScrollView control in Java code. The specific steps are:

(1) Create an Android project in Eclipse named Li_scrollviewjava.

(2) Open the Main.xml layout file in the Res/layout directory, a ScrollView control and a TextView control in the file, whose code is:


<?xml version= "1.0"  encoding= "Utf-8"? ><linearlayout    xmlns:android  =  "http://schemas.android.com/apk/res/android"     android:orientation= "vertical "    android:layout_width=" Match_parent "    android:layout_height=" Match_parent "    android:background=" #000000 ">    <!-- ScrollView is a scrollbar control; Scrollbarstyle is the style of the scroll bar-->    <scrollview         android:layout_width= "Match_parent"          android:layout_height= "200px"         android:scrollbarstyle= " Outsideoverlay "        android:background=" #aabbcc ">         <TextView             android:layout_width= "Match_parent"            android:layout_height= "Wrap_content"             android:id= "@+id/textview"/>     </ScrollView></LinearLayout>


(3) Open the Mainactivity.java file under the Src\fs.li_scrollviewjava package and implement the ScrollView settings in the file with the following code:


package com.duck.shiretan.scrollapplication;import android.app.activity;import  android.os.bundle;import android.widget.textview;public class mainactivity extends  activity {     @Override     protected void oncreate ( Bundle savedinstancestate)  {        super.oncreate ( Savedinstancestate);         setcontentview (R.layout.activity_main);         this.setcontentview (R.layout.activity_main);         settitle ("ScrollView");         textview  textView =  (TextView)  findviewbyid (R.id.textview);         textview.settext ("I am android\n  I am android\n  I am android\n  I am android\n  I am android\n   I'm android\n , I'm android\n , I'm Android.\n  I'm android\n  I'm android\n  I'm android\n  I'm android\n  I'm android\n  I'm android\n  I'm android\n , I'm android\n , I'm android\n , I'm android\n , I'm android\n , I'm android\n . I'm android\n , I'm android\n , I'm android\n , I'm android\n , I'm android\n , I'm android\n . I am android\n ");     }}

Running the above code gets the configuration of the ScrollView control we want to implement.


Welcome to join the Technology Exchange Group: 364595326


Scrolling view of Android view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.