【问题标题】:ConstraintLayout doesn't respect max width/height in combination with DimensionRatioConstraintLayout 不考虑结合 DimensionRatio 的最大宽度/高度
【发布时间】:2017-09-21 09:16:00
【问题描述】:

下面的视图需要是正方形的。我还希望将视图限制在一定的大小。当它与同一元素上的 dimensionRatio 结合使用时,ConstraintLayout 以某种方式忽略了最大宽度/高度属性。

<android.support.constraint.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">
    <View 
       android:layout_width="0dp"
       android:layout_height="0dp"
       app:layout_constraintTop_toTopOf="@+id/guideline_top"
       app:layout_constraintDimensionRatio="1:1"
       app:layout_constraintLeft_toLeftOf="@+id/guideline_left"
       app:layout_constraintRight_toRightOf="@+id/guideline_right"
       app:layout_constraintBottom_toBottomOf="@+id/guideline_bottom" 
       app:layout_constraintWidth_max="100dp"
       app:layout_constraintHeight_max="100dp"/>

目前我正在使用一种解决方法,将视图放入容器中,如下所示。但我宁愿让我的布局层次结构尽可能平坦。

<android.support.constraint.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">
    <android.support.constraint.ConstraintLayout 
          android:layout_width="0dp"
          android:layout_height="0dp"
          app:layout_constraintTop_toTopOf="@+id/guideline_top"
          app:layout_constraintLeft_toLeftOf="@+id/guideline_left"
          app:layout_constraintRight_toRightOf="@+id/guideline_right"
          app:layout_constraintBottom_toBottomOf="@+id/guideline_bottom"
          app:layout_constraintWidth_max="100dp"
          app:layout_constraintHeight_max="100dp"> 
             <View 
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintDimensionRatio="1:1"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent" 
                />

这是约束布局中的一个限制,您不能将最大宽度/高度与尺寸比结合使用吗?

我正在使用 ConstraintLayout v1.0.2。

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    指定任一

    `app:layout_constraintDimensionRatio="H,1:1"` 
    

    `app:layout_constraintDimensionRatio="W,1:1"`
    

    这应该会导致ConstraintLayout 获取您的最大宽度和高度。请参阅文档中的"Ratios"

    【讨论】:

    • 为我工作!谢谢:)
    • 如果我想让它最大化布局使用的空间量,同时保持尺寸比例,没有任何裁剪,所以我不知道是否应该调整宽度或高度?
    • 在设备上不适合我,但在 Android Studio 中预览显示正确的结果,你知道是什么问题吗?
    • @AlexanderPruss 我使用了this solution。您找到其他解决方案了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2022-08-18
    • 2015-02-01
    • 2018-08-21
    相关资源
    最近更新 更多