天天看點

Android ScrollView滾動條

1.ScrollView垂直滾動條

ScrollView控件隻是支援垂直滾動,而且在ScrollView中隻能包含一個控件,通常是在< ScrollView >标簽中定義了一個<LinearLayout>标簽并且在<LinearLayout>标簽中android:orientation屬性值設定為vertical,然後在<LinearLayout>标簽中放置多個控件,如果<LinearLayout>标簽中的控件所占用的總高度超出螢幕的高度,就會在螢幕的右側出現一個滾動條。

注意 :ScrollView的寬和高的設定。

<?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:layout_width="fill_parent"
		android:orientation="vertical" android:layout_height="fill_parent">
		<TextView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="滾動視圖"
			android:textSize="30dp"></TextView>
		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item1"></ImageView>
		<TextView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="顯示多張圖檔"
			android:textSize="30dp"></TextView>
		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item2"></ImageView>
		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item3"></ImageView>
		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item4"></ImageView>
		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item5"></ImageView>
	</LinearLayout>
</ScrollView>
           

2.水準滾動條

HorizontalScrollView控件隻是支援水準滾動,而且它隻能包含一個控件,通常是在< HorizontalScrollView >标簽中定義了一個<LinearLayout>标簽并且在<LinearLayout>标簽中android:orientation屬性值設定為horizontal,然後在<LinearLayout>标簽中放置多個控件,如果<LinearLayout>标簽中的控件所占用的總寬度超出螢幕的寬度,就會出現滾動效果。
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="wrap_content">

	<LinearLayout android:orientation="horizontal"
		android:layout_width="fill_parent" android:layout_height="fill_parent">

		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item1"></ImageView>
		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item2"></ImageView>

		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item3"></ImageView>

		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item4"></ImageView>

		<ImageView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:src="@drawable/item5"></ImageView>
	</LinearLayout>
</HorizontalScrollView>