【问题标题】:Constraint layout Guideline java.lang.AssertionError: BOTTOM约束布局指南 java.lang.AssertionError: BOTTOM
【发布时间】:2020-10-13 02:25:48
【问题描述】:

我有一个 ImageView,我希望它的高度为屏幕高度的 40%。我试图通过约束布局的指南来完成它,但我得到运行时错误“java.lang.AssertionError: BOTTOM”。我认为错误是图像变得高于屏幕的 40%,但我不知道如何修复该错误。这是我的 xml 代码。

<ImageView
    android:id="@+id/apartment_main_image_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:src="@drawable/apart"
    app:layout_constraintBottom_toBottomOf="@+id/first_horizontal_guideline"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<androidx.constraintlayout.widget.Guideline
    android:id="@+id/first_horizontal_guideline"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintGuide_percent="0.4"/>

【问题讨论】:

  • imageViewandroid:layout_width="0dp" android:layout_height="0dp" 中尝试app:layout_constraintBottom_toTopOf="@+id/first_horizontal_guideline" 指南

标签: android imageview android-constraintlayout constraintlayout-guideline


【解决方案1】:

此错误是因为您忘记添加指南的orientation您可以根据您的要求提供指南的方向horizontalvertical

当您使用垂直准线时,任何受其约束的视图都应该水平执行,水平准线也是如此。

<ImageView
    android:id="@+id/apartment_main_image_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:src="@drawable/apart"
    app:layout_constraintBottom_toTopOf="@+id/first_horizontal_guideline"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<androidx.constraintlayout.widget.Guideline
    android:id="@+id/first_horizontal_guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.4"/>

【讨论】:

    【解决方案2】:

    使用app:layout_constraintHeight_percent根据屏幕设置高度,无需使用指南。

    示例代码

     <ImageView
        android:id="@+id/apartment_main_image_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/apart"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHeight_percent="0.4"
        app:layout_constraintTop_toTopOf="parent" />
    

    【讨论】:

      【解决方案3】:

      您没有添加 android:orientation="horizo​​ntal" 属性使用此代码

      <androidx.constraintlayout.widget.Guideline
              android:id="@+id/first_horizontal_guideline"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintGuide_percent="0.40" />  
      

      【讨论】:

        【解决方案4】:

        使用布局约束高度百分比设置高度并删除从下到下的命令 要么 你需要 40% 的高度,所以你可以通过约束高度百分比来做到这一点,并使宽度匹配父项

        For more info and clarification check out the link

        【讨论】:

          猜你喜欢
          • 2021-04-09
          • 2019-01-06
          • 1970-01-01
          • 1970-01-01
          • 2021-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-25
          相关资源
          最近更新 更多