1. 程式人生 > >Android-scrollview裡的textview無法撐滿螢幕解決方案

Android-scrollview裡的textview無法撐滿螢幕解決方案

ScrollView裡只放一個元素.

當ScrollView裡的元素想填滿ScrollView時,使用”fill_parent”是不管用的,必需為ScrollView設定:android:fillViewport=”true”。

當ScrollView沒有fillVeewport=“true”時, 裡面的元素(比如LinearLayout)會按照wrap_content來計算(不論它是否設了”fill_parent”),而如果LinearLayout的元素設定了fill_parent,那麼也是不管用的,因為LinearLayout依賴裡面的元素,而裡面的元素又依賴LinearLayout,這樣自相矛盾.所以裡面元素設定了fill_parent,也會當做wrap_content來計算.

示例:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/hahhahahaha" /> </LinearLayout
>
</ScrollView>

效果如圖:

這裡寫圖片描述