Android development advanced components - ScrollView (scroll view component)

1. The height of the mobile phone screen is limited. When multiple sets of information need to be displayed, the ScrollView view can reasonably arrange these components, and the browsing can be automatically scrolled. ScrollView is a component that implements scrolling, as long as the components that need scrolling are added to ScrollView. ScrollView supports vertical scrolling, and HorizontalScrollView supports horizontal scrolling.

2. The ScrollView hierarchy is as follows:
   java.lang.Object
      android.view.View
         android.view.ViewGroup
            android.widget.FrameLayout
               android.widget.ScrollView

3. The ScrollView component can be set in the code or in the XML layout file The usage form is similar to the operation form of the layout manager. The difference is that the layout manager can contain multiple components, while the scroll view can only have one component, and this component can accommodate components with excess screen height.

4. Create a new XML file:
<?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:id="@+id/scroll">

    <LinearLayout
        android:id="@+id/layout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        </LinearLayout>

</ScrollView>

5、修改MainActivity.java文件
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
* Created by xiao on 2016/12/26.
*/
public class MainActivity extends Activity {

    private String scrollData[] = {"信息学院", "School of Mechanical Engineering", "School of Computer Science", "School of Journalism", "School of Chemical Engineering",
            "School of Fine Arts", "School of Computer", "School of Journalism", "School of Chemical Engineering", "School of Fine Arts", "School of Physical Education", "School of Music",
            "School of Economics and Management", "South Lake College", "Physics and Electronics" College", "Mechatronics College", "Law College", "Foreign Language College",
            "Science and Technology Office", "Library", "Academic Affairs Office", "Network Center", "Academic Engineering Office", "Finance Office"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scrollview_xml);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        for(int i = 0; i < scrollData.length; i++){
            TextView msg = new TextView(this);
            msg.setTextSize(20);
            msg.setText(scrollData[i]);
            layout.addView(msg, params);
        }
    }
}

6. Generally, event monitoring is not added in ScrollView. The component needs to have a responsive event, which is generally implemented using the ListView component.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326360510&siteId=291194637
Recommended