xml 布局中的 ScrollView?

ScrollView in xml layout?

我在我的布局中使用 ScrollView 是这样的:

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <!-- my rest xml code-->
    </LinearLayout>

</ScrollView>

然后我的布局预览显示如下:

为什么布局是这样的?!

假设您的问题是尽管您将子布局高度设置为 match_parent,但视图缩小了。使其正确。您已将 fillViewport 添加到 true.

例如:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:fillViewport=true
        android:layout_height="match_parent">
        //child 
</ScrollView>

阅读文档here.

这只是因为您使用 ScrollView 作为根视图。

根据需要更改根视图(LinearLayout/RelativeLayout/FrameLayout)并在根视图下添加滚动视图作为子布局。

例如

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent" > 
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <!-- your rest xml code-->
    </LinearLayout>

</ScrollView>
</LinearLayout>

希望对您有所帮助。

// you can use the scrollview like below code
//where your scroll view must conatin a root view contain all child vies

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".uiApplication.hmactivties">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bgsecond"
    android:orientation="vertical">
  </RelativeLayout>