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>

效果如下,自动全屏,非常完美:
在这里插入图片描述

发布了414 篇原创文章 · 获赞 287 · 访问量 57万+

猜你喜欢

转载自blog.csdn.net/woshisangsang/article/details/105114741