【问题标题】:How to make only part of the layout scrollable with ScrollView and Linear Layout?如何使用 ScrollView 和 Linear Layout 仅使部分布局可滚动?
【发布时间】:2020-05-12 12:14:51
【问题描述】:

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

.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>

【问题讨论】:

    标签: android android-linearlayout android-scrollview scrollable


    【解决方案1】:

    看看你是怎么做到的......忘记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>
    

    【讨论】:

    • 谢谢,我不知道水平滚动视图,之后我会测试并选择你的答案。
    • 在您的 .xml 中,谁是最重要的(顶部)布局并且覆盖了整个代码?包含属性 xmlns: android = "schemas.android.com/apk/res/android" xmlns: tools = "schemas.android.com/tools" xmlns: app = "schemas.android.com/apk/res-auto"
    • 我得到了“水平滚动视图”,非常感谢
    • @lukecross “在你的 .xml 中,谁是最重要的布局(顶部)”,它是主要的 LinearLayout。我没有在这里添加它,但这就是我提到它的原因......
    【解决方案2】:

    您必须添加另一个布局来放置您的滚动视图...但是您的 XML 结构并不好。首先,您必须有 2 个 LinearLayouts,第一个用于带有 CardViews 的水平 ScrollView,第二个用于带有 XML 其余部分的垂直 ScrollView...如果您需要更多帮助,请告诉我!

    【讨论】:

    • 那么我的布局应该以 .xml 开头吗?亲戚?
    • 每当我使用 ScrollView 时,我只使用 FrameLayout
    • 那么,我一定要将水平和垂直可滚动分离为2帧布局吗?
    • @lukecross,看看我刚刚发布的内容......所以作为主布局使用LinearLayout,然后尝试按照我发布的代码进行操作。祝你好运
    【解决方案3】:

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

    【讨论】:

    • 这个例子是外部库吗?
    猜你喜欢
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多