問題引入:手機(jī)屏幕的顯示空間有限,常常需要上下滑動(dòng)或左右滑動(dòng)才能拉出其余頁面內(nèi)容,可惜一般的布局節(jié)點(diǎn) 都不支持自行滾動(dòng),這時(shí)就要借助滾動(dòng)視圖了。與線性布局類似,滾動(dòng)視圖也分為垂直方向和水平方向 兩類,其中垂直滾動(dòng)視圖名為ScrollView,水平滾動(dòng)視圖名為
HorizontalScrollView
這兩個(gè)滾動(dòng)視圖的 使用并不復(fù)雜,
主要注意以下3點(diǎn):
(1)垂直方向滾動(dòng)時(shí),layout_width屬性值設(shè)置為match_parent,layout_height屬性值設(shè)置為wrap_content。
(2)水平方向滾動(dòng)時(shí),layout_width屬性值設(shè)置為wrap_content,layout_height屬性值設(shè)置為match_parent。
(3)滾動(dòng)視圖節(jié)點(diǎn)下面必須且只能掛著一個(gè)子布局節(jié)點(diǎn),否則會(huì)在運(yùn)行時(shí)報(bào)錯(cuò)。
Caused by:java.lang.IllegalStateException:ScrollView can host only one direct child
下面是垂直滾動(dòng)視圖ScrollView和水平滾動(dòng)視圖HorizontalScrollView的XML例子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:layout_width="match_parent"
? ?android:layout_height="match_parent"
? ?android:orientation="vertical">
? ?<!-- HorizontalScrollView是水平方向的滾動(dòng)視圖,當(dāng)前高度為200dp -->
? ?<HorizontalScrollView
? ? ? ?android:layout_width="wrap_content"
? ? ? ?android:layout_height="200dp">
? ? ? ?<!-- 水平方向的線性布局,兩個(gè)子視圖的顏色分別為青色和黃色 -->
? ? ? ?<LinearLayout
? ? ? ? ? ?android:layout_width="wrap_content"
? ? ? ? ? ?android:layout_height="match_parent"
? ? ? ? ? ?android:orientation="horizontal">
? ? ? ? ? ?<View
? ? ? ? ? ? ? ?android:layout_width="300dp"
? ? ? ? ? ? ? ?android:layout_height="match_parent"
? ? ? ? ? ? ? ?android:background="#aaffff" />
? ? ? ? ? ?<View
? ? ? ? ? ? ? ?android:layout_width="300dp"
? ? ? ? ? ? ? ?android:layout_height="match_parent"
? ? ? ? ? ? ? ?android:background="#ffff00" />
? ? ? ?</LinearLayout>
? ?</HorizontalScrollView>
?<!-- ScrollView是垂直方向的滾動(dòng)視圖,當(dāng)前高度為自適應(yīng) -->
? ?<ScrollView
? ? ? ?android:layout_width="match_parent"
? ? ? ?android:layout_height="wrap_content">
? ? ? ?<!-- 垂直方向的線性布局,兩個(gè)子視圖的顏色分別為綠色和橙色 -->
? ? ? ?<LinearLayout
? ? ? ? ? ?android:layout_width="match_parent"
? ? ? ? ? ?android:layout_height="wrap_content"
? ? ? ? ? ?android:orientation="vertical">
? ? ? ? ? ?<View
? ? ? ? ? ? ? ?android:layout_width="match_parent"
? ? ? ? ? ? ? ?android:layout_height="400dp"
? ? ? ? ? ? ? ?android:background="#00ff00" />
? ? ? ? ? ?<View
? ? ? ? ? ? ? ?android:layout_width="match_parent"
? ? ? ? ? ? ? ?android:layout_height="400dp"
? ? ? ? ? ? ? ?android:background="#ffffaa" />
? ? ? ?</LinearLayout>
? ?</ScrollView>
</LinearLayout>
運(yùn)行測(cè)試App,可知ScrollView在縱向滾動(dòng),而HorizontalScrollView在橫向滾動(dòng)。 有時(shí)ScrollView的實(shí)際內(nèi)容不夠,又想讓它充滿屏幕,怎么辦呢?如果把layout_height屬性賦值為match_parent,結(jié)果還是不會(huì)充滿,正確的做法是再增加一行屬性android:fillViewport(該屬性為true表示允許填滿視圖窗口),屬性片段舉例如下:
android:layout_height="match_parent"
android:fillViewport="true"
運(yùn)行結(jié)果:
注:運(yùn)行前修改清單文件
滑動(dòng)頁面文章來源:http://www.zghlxwxcb.cn/news/detail-410471.html
感謝觀看!??!文章來源地址http://www.zghlxwxcb.cn/news/detail-410471.html
到了這里,關(guān)于【Android Studio】常用布局 --- 滾動(dòng)視圖ScrollView的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!