ScrollView使用fillViewport设置高度为MatchParent

时间:2023-02-09 20:17:44

转载请注明出处:http://blog.csdn.net/u012975705/article/details/49275001

之前遇到一问题,就是当使用ScrollView时,ScrollView控件里的布局无法充满整个手机屏幕,通过阅读源码发现,ScrollView中有个mFillViewport属性:
ScrollView使用fillViewport设置高度为MatchParent

When set to true, the scroll view measure its child to make it fill the currently visible area.

大概意思就是当mFillViewport设置为true的时候,其子视图能够自动填充满当前可见的整个区域。即,在xml布局文件中为ScrollView控件添加

android:fillViewport="true"

能使ScrollView里的元素填满整个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:background="@color/white"
android:gravity="center"
android:orientation="vertical">


<ScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">


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


<TextView
style="@style/TextTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:text="手机耕犊" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:orientation="vertical">


<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:orientation="horizontal">


<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:src="@drawable/ic_phone" />


<EditText
android:id="@+id/login_telnum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="@string/pc_telnum"
android:paddingLeft="15dp"
android:singleLine="true"
android:textColor="@color/text_title"
android:textColorHint="@color/gray"
android:textCursorDrawable="@null"
android:textSize="@dimen/typeface_medium_16" />

</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:background="@color/bg_custom_gray" />


<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:orientation="horizontal">


<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:src="@drawable/ic_lock" />


<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="@string/pc_password"
android:paddingLeft="15dp"
android:password="true"
android:singleLine="true"
android:textColor="@color/text_title"
android:textColorHint="@color/gray"
android:textCursorDrawable="@null"
android:textSize="@dimen/typeface_medium_16" />

</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:background="@color/bg_custom_gray" />


<TextView
android:id="@+id/pc_forget_pwd"
style="@style/TextLittle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="@string/pc_forget_password"
android:textColor="#20ac42"
android:textSize="12dp" />

</LinearLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp">


<Button
android:id="@+id/pc_login"
android:layout_width="match_parent"
android:layout_height="@dimen/common_button_height"
android:background="@drawable/bg_bt_rectangle_green2"
android:text="@string/pc_login"
android:textColor="@color/white"
android:textSize="@dimen/typeface_medium_16" />


</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">


<TextView
android:id="@+id/pc_register"
android:layout_width="match_parent"
android:layout_height="@dimen/common_button_height"
android:background="@color/bg_custom_gray"
android:gravity="center"
android:text="@string/pc_register"
android:textColor="@color/gray"
android:textSize="@dimen/typeface_small_14" />

</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>