首页 > 解决方案 > 防止布局被推过某个边界

问题描述

我有一个 LinearLayout

app:layout_constraintDimensionRatio="1:2"
android:layout_width="match_parent"
app:layout_constraintBottom_toBottomOf="parent"

在 ConstraintLayout 中。如果屏幕高度大于屏幕宽度的两倍,它看起来应该是这样。然而,在更宽的设备上,我的布局顶部被推出了屏幕。但是我希望顶部保持可见并让底部移出屏幕。这是我希望布局的行为方式:

1:足够长的设备,一切都很好:

长装置

2:设备稍宽一点,但一切正常:

屏幕比例为 1:2

3:更宽的设备,布局的顶部应该仍然可见,底部应该向下推:

更宽的屏幕

标签: androidandroid-layoutandroid-constraintlayout

解决方案


已解决:我确实将它放入 aScrollView但为了app:layout_constraintDimensionRatio也可以工作,我需要将实际内容放入 a ConstraintLayoutin 中ScrollView

            <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="bottom"
                android:orientation="vertical">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/layoutBgSnakes"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/imgView"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        app:layout_constraintBottom_toTopOf="@id/imgView2"
                        app:layout_constraintDimensionRatio="1:1"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:srcCompat="@drawable/..." />

                    <ImageView
                        android:id="@+id/imgView2"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintDimensionRatio="1:1"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:srcCompat="@drawable/..." />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </RelativeLayout>
        </ScrollView>

注意: android:fillViewport="true"是使一切正常的关键线。


推荐阅读