如何使用 ScrollView 和 Linear Layout 仅使部分布局可滚动?

How to make only part of the layout scrollable with ScrollView and Linear Layout?

我有一个 layout.xml 分为两部分,其中一部分包括垂直填充的字段,另一部分(初始部分)包括 3 个将用于填充的卡片视图imageView(稍后),然而,当试图只让 cardviews 水平滚动时,没有任何反应,唯一发生的是整个布局垂直滚动,但没有任何 cardviews 水平滚动。

.xml 存档代码:

    <?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="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:scrollbars="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:background="#FAFAFA">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar4"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:titleTextColor="@android:color/white"
            android:background="@color/colorPrimary" />
    </com.google.android.material.appbar.AppBarLayout>
    <com.google.android.material.textview.MaterialTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Adicione até 3 fotos do seu produto"
        android:fontFamily="@font/bevan"
        android:gravity="center"/>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <com.google.android.material.card.MaterialCardView
            android:id="@+id/anuncioImg1"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_margin="6dp">
            <com.google.android.material.textview.MaterialTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="+"/>
        </com.google.android.material.card.MaterialCardView>
        <com.google.android.material.card.MaterialCardView
            android:id="@+id/anuncioImg2"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_margin="6dp">
            <com.google.android.material.textview.MaterialTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="+"/>
        </com.google.android.material.card.MaterialCardView>
        <com.google.android.material.card.MaterialCardView
            android:id="@+id/anuncioImg3"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_margin="6dp">
            <com.google.android.material.textview.MaterialTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="+"/>
        </com.google.android.material.card.MaterialCardView>
        <com.google.android.material.card.MaterialCardView
            android:id="@+id/anuncioImg4"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_margin="6dp">
            <com.google.android.material.textview.MaterialTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="+"/>
        </com.google.android.material.card.MaterialCardView>
            <com.google.android.material.card.MaterialCardView
                android:id="@+id/anuncioImg5"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_margin="6dp">
                <com.google.android.material.textview.MaterialTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="+"/>
            </com.google.android.material.card.MaterialCardView>
        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="Título do Anúncio:"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" />
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Ex: Gado Nelore 300Kg"
        app:startIconDrawable="@drawable/ic_text_fields_24dp"
        app:startIconTint="@color/colorPrimary"
        app:helperTextEnabled="true"
        app:helperTextTextColor="@android:color/black"
        app:helperTextTextAppearance="@style/TextAppearance.AppCompat.Caption"
        app:boxBackgroundColor="@android:color/white"
        app:counterEnabled="true"
        app:counterMaxLength="30"
        app:counterTextColor="@color/colorPrimary">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/tituloAnuncio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textShortMessage"
            android:maxLength="30" />
    </com.google.android.material.textfield.TextInputLayout>
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="Descrição:"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" />
        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Ex: Gado 20 meses de vida, 10 arrobas..."
            app:startIconDrawable="@drawable/ic_short_text_24dp"
            app:startIconTint="@color/colorPrimary"
            app:helperTextEnabled="true"
            app:helperTextTextColor="@android:color/black"
            app:helperTextTextAppearance="@style/TextAppearance.AppCompat.Caption"
            app:boxBackgroundColor="@android:color/white"
            app:counterEnabled="true"
            app:counterMaxLength="240"
            app:counterTextColor="@color/colorPrimary">
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editDescAnuncio"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"
                android:maxLength="240"/>
        </com.google.android.material.textfield.TextInputLayout>
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="Preço:"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" />
        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Ex: 1650"
            app:startIconDrawable="@drawable/ic_monetization_on"
            app:startIconTint="@color/colorPrimary"
            app:helperTextEnabled="true"
            app:helperTextTextColor="@android:color/black"
            app:helperTextTextAppearance="@style/TextAppearance.AppCompat.Caption"
            app:boxBackgroundColor="@android:color/white"
            app:counterEnabled="true"
            app:counterMaxLength="6"
            app:counterTextColor="@color/colorPrimary"
            app:prefixText="R$"
            app:prefixTextColor="@android:color/black">
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editPrecoAnuncio"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberSigned"
                android:maxLength="6" />
        </com.google.android.material.textfield.TextInputLayout>
        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="Telefone do anunciante:"
            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" />
        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Ex: 64999999999, apenas números"
            app:startIconDrawable="@drawable/fui_ic_phone_white_24dp"
            app:startIconTint="@color/colorPrimary"
            app:helperTextEnabled="true"
            app:helperTextTextColor="@android:color/black"
            app:helperTextTextAppearance="@style/TextAppearance.AppCompat.Caption"
            app:boxBackgroundColor="@android:color/white"
            app:counterEnabled="true"
            app:counterMaxLength="11"
            app:counterTextColor="@color/colorPrimary">
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/numeroAnuncio"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:maxLength="11" />
        </com.google.android.material.textfield.TextInputLayout>
    </LinearLayout>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/textview_addAnuncio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:backgroundTint="@android:color/white"
            android:padding="8dp"
            android:layout_gravity="bottom"
            android:fontFamily="@font/baumans"
            android:text="@string/avisoVenda"
            android:textColor="@android:color/black"
            android:textSize="13sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>

您必须添加另一个布局来放置您的 scrollView...但是您的 XML 结构不好。首先,您必须有 2 个 LinearLayouts,第一个用于水平 ScrolView 和 CardView,第二个用于垂直 ScrolView 和其余 XML...如果您需要更多帮助,请告诉我!

ScrollView 在另一个 ScrollView 中的行为不可预测,您应该尝试 NestedScrollingParent2 和 NestedScrollingChild2。

看看你是怎么做到的...忘记 FrameLayout,使用 Linear 作为主要布局

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:background="@drawable/avli_horse"/>
            <ImageView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:background="@drawable/bar"/>
            <ImageView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:background="@drawable/kitchen"/>
            <ImageView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:background="@drawable/avli_logo"/>

        </GridLayout>

    </HorizontalScrollView>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:orientation="vertical"
    android:layout_weight="3">



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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@drawable/avli_horse" />

        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@drawable/bar" />

        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@drawable/kitchen" />

        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:background="@drawable/avli_logo" />


    </LinearLayout>

</ScrollView>

</LinearLayout>