Android Studio 滾動視圖ScrollView使用與全屏實例

使用場景

如果一個頁面內容很多,比如個人信息註冊頁面,需要往下(或者左右)滑動才能顯示全內容,可以使用滾動視圖。

註冊頁面實例

代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="4dp">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="400dp">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="4dp">
            <EditText
                android:id="@+id/editTextName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="姓名" />
            <EditText
                android:id="@+id/editTextPasword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="密碼" />
            <EditText
                android:id="@+id/editTextEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="郵箱" />
            <EditText
                android:id="@+id/editTextSchool"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="學校" />
            <EditText
                android:id="@+id/editTexAge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="年齡" />
        </LinearLayout>
    </ScrollView>
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="註冊" />
</LinearLayout>

效果如下:
在這裏插入圖片描述

ScrollView填滿窗口

有時候不好把握屏幕高度,所以希望視圖能自動填滿窗口,藉助fillViewport即可輕易實現。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="4dp">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="4dp">
            <EditText
                android:id="@+id/editTextName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="姓名" />
            <EditText
                android:id="@+id/editTextPasword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="密碼" />
            <EditText
                android:id="@+id/editTextEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="郵箱" />
            <EditText
                android:id="@+id/editTextSchool"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="學校" />
            <EditText
                android:id="@+id/editTexAge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="3"
                android:text="年齡" />
            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="註冊" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

效果如下,自動全屏,非常完美:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章