滚动视图无法滚动

huangapple go评论47阅读模式
英文:

The scrollview doesn't scroll

问题

I'm trying to use ScrollView but it is not working for me whatsoever.



        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="20"
            android:columnCount="2"
            android:rowCount="3"
            android:alignmentMode="alignMargins"
            android:columnOrderPreserved="false"
            android:padding="15dp">
            <androidx.cardview.widget.CardView
                ......

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_marginTop="15dp"
                    android:orientation="vertical">
                    <TextView
                      ...>
                    </TextView>
                    <ImageView
                       ...>
                    </ImageView>
                </LinearLayout>
            </androidx.cardview.widget.CardView>
        </GridLayout>

    </LinearLayout>
</ScrollView>

I tried every solution I found online, but nothing worked.
I tried to add this:
android:orientation="vertical"
to the linearLayout, but it changed nothing. I still can't scroll
How can I get the scrollView to work?

英文:

I'm trying to use Scrollview but it is not working for me whatsoever.

&lt;LinearLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:id=&quot;@+id/top&quot;
    android:orientation=&quot;vertical&quot;
    tools:context=&quot;.ui.home.HomeFragment&quot;&gt;
    &lt;ScrollView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:animateLayoutChanges=&quot;true&quot;
        android:layout_marginTop=&quot;?attr/actionBarSize&quot;
        android:fillViewport=&quot;true&quot;&gt;
    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;#ffffff&quot;
        android:orientation=&quot;vertical&quot;
        android:weightSum=&quot;10&quot;&gt;
      

        &lt;GridLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;0dp&quot;
            android:layout_weight=&quot;20&quot;
            android:columnCount=&quot;2&quot;
            android:rowCount=&quot;3&quot;
            android:alignmentMode=&quot;alignMargins&quot;
            android:columnOrderPreserved=&quot;false&quot;
            android:padding=&quot;15dp&quot;
            &gt;
            &lt;androidx.cardview.widget.CardView
                .....
                &gt;
                &lt;LinearLayout
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;match_parent&quot;
                    android:layout_gravity=&quot;center_horizontal|center_vertical&quot;
                    android:layout_marginTop=&quot;15dp&quot;
                    android:orientation=&quot;vertical&quot;
                    &gt;
                    &lt;TextView
                      ...&gt;

                    &lt;/TextView&gt;
                    &lt;ImageView
                       ...&gt;
                    &lt;/ImageView&gt;
                &lt;/LinearLayout&gt;
            &lt;/androidx.cardview.widget.CardView&gt;

&lt;/GridLayout&gt;

    &lt;/LinearLayout&gt;
    &lt;/ScrollView&gt;
&lt;/LinearLayout&gt;

I tried every solution I found online, but nothing worked.
I tried to add this :
android:orientation="vertical"
to the linearLayout, but it changed nothing. I still can't scroll
How can I get the scrollView to work?

答案1

得分: 1

将您的布局放在ScrollView下面,就像这样:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ScrollActivity">

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/> <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/> <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/> <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/> <!-- 中间省略了很多Button,以保持简洁 -->

    </LinearLayout>

</ScrollView>

确保您的子布局具有足够的视图,以便可以滚动。

希望这对您解决问题有所帮助 滚动视图无法滚动

英文:

Put your layout under the ScrollView Like this

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;ScrollView
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.ScrollActivity&quot;&gt;

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:orientation=&quot;vertical&quot;
        android:layout_height=&quot;wrap_content&quot;&gt;

        &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt; &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;/&gt;

    &lt;/LinearLayout&gt;

  &lt;/ScrollView&gt;

Make sure that your Child layout has enough views so that can scrollable.

I hope this can helps you out from your problem ):

答案2

得分: 0

尝试替换如下

android:layout_width="fill_parent"
android:layout_height="fill_parent"
英文:

Try replacing like this

android:layout_width=&quot;fill_parent&quot;
android:layout_height=&quot;fill_parent&quot;

huangapple
  • 本文由 发表于 2023年2月18日 15:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定