Android中幾個常用的Layout雖然有scrollbar屬性,但是並不能實現當其中內容太多時自動在Activity上出現Scrollbar,後來發現對於這種情況其實是需要使用ScrollView來處理的,具體配置可以如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id_scrollbar/layout1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > ... </LinearLayout> </ScrollView> </LinearLayout>
或者
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > ... </LinearLayout> </ScrollView>
其中需要注意的是ScrollView內部只能有一個子項目,所以需要把所有的子項目放到一個LinearLayout內部。