In this example, ScrollView is used to display a long list, and its defined scroll_view_2.xml
[Html]
<ScrollView xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: scrollbars = "none">
<LinearLayout
Android: id = "@ + id/layout"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
<TextView
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/scroll_view_2_text_1"/>
<Button
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/scroll_view_2_button_1"/>
</LinearLayout>
</ScrollView>
<ScrollView xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: scrollbars = "none">
<LinearLayout
Android: id = "@ + id/layout"
Android: orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
<TextView
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/scroll_view_2_text_1"/>
<Button
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/scroll_view_2_button_1"/>
</LinearLayout>
</ScrollView>
ScrollView only defines one TextView and Button in LinearLayout, while in ScrollView2.java, 63 textviews and buttons are dynamically added to LinearLayout using code:
[Java]
LinearLayout layout
= (LinearLayout) findViewById (R. id. layout );
For (int I = 2; I <64; I ++ ){
TextView textView = new TextView (this );
TextView. setText ("Text View" + I );
LinearLayout. LayoutParams p
= New LinearLayout. LayoutParams (
LinearLayout. LayoutParams. MATCH_PARENT,
LinearLayout. LayoutParams. WRAP_CONTENT
);
Layout. addView (textView, p );
Button buttonView = new Button (this );
ButtonView. setText ("Button" + I );
Layout. addView (buttonView, p );
}
LinearLayout layout
= (LinearLayout) findViewById (R. id. layout );
For (int I = 2; I <64; I ++ ){
TextView textView = new TextView (this );
TextView. setText ("Text View" + I );
LinearLayout. LayoutParams p
= New LinearLayout. LayoutParams (
LinearLayout. LayoutParams. MATCH_PARENT,
LinearLayout. LayoutParams. WRAP_CONTENT
);
Layout. addView (textView, p );
Button buttonView = new Button (this );
ButtonView. setText ("Button" + I );
Layout. addView (buttonView, p );
}
To form a long list, a screen cannot be displayed, and you can scroll the screen to display it: