ScrollView无法滚动 - Android

时间:2015-05-17 01:57:14

标签: android android-layout imageview scrollview android-linearlayout

我无法理解为什么会这样。我无法滚动我的滚动视图。它有一个textView,一个imageview和几个线性布局。当我用textview替换imageview和线性布局时它正在工作。这真的很不寻常和令人沮丧。谁能帮我?这是我的代码:

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

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Drop Text Down"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="555dp"
            android:src="@drawable/ic_launcher" />

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

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ln"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#000000"
            android:orientation="vertical" >

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

                <TextView
                    android:id="@+id/TextView06"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView05"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView04"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView03"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

                <TextView
                    android:id="@+id/TextView01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
            </LinearLayout>

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

</LinearLayout>

感谢。

8 个答案:

答案 0 :(得分:12)

ScrollView的子视图应设置为wrap_content。如果您将其设置为match_parent,它将填充ScrollView的区域并且永不滚动,因为它不会大于ScrollView

尝试将子LinearLayout layout_height更改为wrap_content或特定大小(以dp为单位)而不是match_parent。

答案 1 :(得分:12)

放置一个固定高度的空视图

<View
                android:layout_width="match_parent"
                android:layout_height="50dp" />

作为线性布局中的最后一项,它是滚动视图的子项..

这对我有用..

答案 2 :(得分:2)

您的ScrollView孩子的身高必须为wrap_content

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:orientation="vertical"
    android:scrollbars="vertical" >

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

答案 3 :(得分:1)

您应该将LinearLayout(Scrollview的子级)的高度设置为wrap_content。

当孩子比ScrollView高时,android:fillViewport =“true”属性没有效果。

答案 4 :(得分:1)

我可以通过在活动清单中添加(android:windowSoftInputMode =“ adjustResize | stateHidden”)来解决此问题,如下所示

<activity
            android:name=".ui.main.MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
        </activity> 

答案 5 :(得分:1)

也许我为时已晚,但帮助我修复滚动视图在经过数小时的搜索和反复试验后不滚动的唯一方法是将滚动视图的高度设置为 0dp。

这里有一个示例 xml 代码供参考。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
  • 具有“match_parent”的顶部约束布局将适合屏幕。
  • 内部具有“0dp”的滚动视图调整内部内容的高度,并在内容超出顶部/父约束布局时创建可滚动视图。
  • 最内部的约束布局(滚动视图的子级)将包含视图和实际内容。

希望这会有所帮助。祝你有美好的一天:)

答案 6 :(得分:-2)

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

从元素

中删除android:fillViewport =“true”

答案 7 :(得分:-3)

使用表格布局而不是像这样的线性布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:fadeScrollbars="false" android:padding="6dip">
<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1"
    >   
   //your stuff 
</TableLayout>
</ScrollView>