【问题标题】:Android app:layout_constraintDimensionRatio not working within ScrollView->LinearLayout containerAndroid 应用程序:layout_constraintDimensionRatio 在 ScrollView->LinearLayout 容器中不起作用
【发布时间】:2021-04-28 00:04:34
【问题描述】:

我正在尝试在 ScrollView 元素中创建一个 ID 为 imgContainer 的方形 LinearLayout 容器,但我无法这样做,因为 app:layout_constraintDimensionRatio 似乎不起作用。使用以下 xml 代码,imgContainer 以 0dp 的高度呈现:

<?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">

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

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

            <LinearLayout
                android:id="@+id/imgContainer"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp"
                android:orientation="vertical"
                app:layout_constraintDimensionRatio="1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/trackName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="8dp"
                android:layout_weight="1"
                android:text="Test Name"
                android:textSize="25dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imgContainer" />

              <!-- Other stuff -->

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

但是,当我像这样将 imgContainer 从 ScrollView 中移出时,imgContainer 就像预期的那样是方形的。

<?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">

    <LinearLayout
        android:id="@+id/imgContainer"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

     <ScrollView
        ...>
        <!-- Same as above -->
     </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

我还尝试移出它嵌套在其中的 LinearLayout,同时仍将它放在 ScrollView 中,但这也不起作用。如果有人知道如何解决这个问题,以及为什么会发生这种情况,那就太好了。提前致谢。

【问题讨论】:

  • 恐怕这不是视图组的工作方式。每个视图组只布置它的直接子级,因此在线性布局的子级上使用约束布局属性没有任何意义。
  • 好的,谢谢。我想我会尝试另一种方式。
  • 使用ConstraintLayout,您通常根本不需要任何其他视图组。它的全部意义在于足够强大,可以以更复杂的方式布局子级。在这里你应该只需要ScrollView -&gt; ConstraintLayout

标签: android xml kotlin


【解决方案1】:

app:layout_constraint.. 仅适用于

您可以尝试以下方法:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/imgContainer"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <TextView
            android:id="@+id/trackName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="8dp"
            android:layout_weight="1"
            android:text="Test Name"
            android:textSize="25dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imgContainer" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

【讨论】:

    猜你喜欢
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多