【问题标题】:Android: ScrollView containing two nested ConstraintLayouts, cant fill screen horizontallyAndroid:ScrollView包含两个嵌套的ConstraintLayout,无法水平填满屏幕
【发布时间】:2020-11-19 16:20:27
【问题描述】:

我遇到以下 XML 问题:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <View
                android:id="@+id/block"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <LinearLayout
                android:id="@+id/linearLayout"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="#419E9E9E"
                android:orientation="horizontal"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                app:layout_constraintTop_toBottomOf="@id/block"></LinearLayout>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/holder"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/linearLayout">

                <TextView
                    android:id="@+id/holderText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="50dp"
                    android:layout_marginLeft="50dp"
                    android:layout_marginTop="12sp"
                    android:gravity="center_vertical"
                    android:maxLines="4"
                    android:text="Hello World"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="24sp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/holder" />

                <Button
                    android:id="@+id/Button"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginEnd="20dp"
                    android:layout_marginBottom="20dp"
                    android:text="Button"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>


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

</androidx.constraintlayout.widget.ConstraintLayout>

垂直地,布局做我想要的:它放置最后一个布局并填满屏幕。 但是,当设备水平翻转时,由于app:layout_constraintBottom_toBottomOf="parent"对自身的约束,底部(嵌套的constraintlayout)消失了

“如何在保持水平滚动的同时用第二个布局填充屏幕?”

我已附上图片和演示库以隔离问题。

链接: https://github.com/taesookim0412/StackOverflow_Question_Android_NestedConstraintLayouts_ScrollView

【问题讨论】:

    标签: android xml scrollview


    【解决方案1】:

    我找到了解决方案。代替 minHeight,您可以使用

    app:layout_constraintHeight_min="100dp"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      相关资源
      最近更新 更多