【问题标题】:ConstraintLayout layout_constraintDimensionRatio not workingConstraintLayout layout_constraintDimensionRatio 不起作用
【发布时间】:2017-05-20 05:32:41
【问题描述】:

我使用了constraintLayoutlayout_constraintDimensionRatio="1:1" (宽度为wrap_content,高度为0dp (match_constraint))

因此,我预计宽度和高度为 1:1,但它不起作用。

怎么了?

我附上了代码和截图。

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:text="Hello World!11"
        app:layout_constraintDimensionRatio="1:1" />

</android.support.constraint.ConstraintLayout>

screenshot

我引用了有关 Constraintlayout 的 android 开发者网站。 https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints

Ratio :: 您还可以将小部件的一维定义为 另一个。为了做到这一点,你需要至少有一个 将约束维度设置为 0dp(即 MATCH_CONSTRAINT),并设置 属性 layout_constraintDimentionRatio 到给定的比率。为了 示例:

     <Button android:layout_width="wrap_content"
               android:layout_height="0dp"
               app:layout_constraintDimensionRatio="1:1" />
     
  will set the height of the button to be the same as its width.

但它不起作用。

【问题讨论】:

  • ConstrainLayout的版本是多少?
  • 嗨,Shailesh。我用的是 1.0.2
  • match_parent 不受支持。你需要设置0dp 所以ConstraintLayout 相应地设置它。检查这个stackoverflow.com/questions/37603751/…
  • 如果您想创建,那么您可以通过其他方式进行。检查这个stackoverflow.com/questions/37318228/…
  • @Shailesh 谢谢评论。我知道不支持 match_parent。但它仅适用于直接在 ConstraintLayout 下的视图(不确定..)

标签: android android-constraintlayout


【解决方案1】:

您忘记添加约束

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

    <TextView
        android:id="@+id/t1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:text="Hello World!11"
        app:layout_constraintDimensionRatio="1" />

</android.support.constraint.ConstraintLayout>

0dp 仅适用于 ConstraintLayout 的子视图。 任何视图都应该应用其父级的属性规则。

【讨论】:

  • 我忘了我将宽度设置为 0dp。但是,我不知道为什么Android开发者网站上的示例显示它只有在我插入时才有效。
【解决方案2】:

从 1.1.0 版本开始,这已经改变了。

您现在可以定义:

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

查看以下链接以查找有关 DimensionConstraints 的所有文档:

Link to the docs

【讨论】:

    【解决方案3】:

    在我的情况下,我遇到了一个问题,比如我必须用 A4 大小的纸张比例填充容器内的布局。

    问题

    我从后端获取 A4 大小的简历页面作为图像,因此我必须将这些图像附加到 Viewpager 中,在其中我使用 ImageView 来显示这些图像。

    解决方案

    我浏览了Constraint layout 文档,其中有Ratio 部分。所以我可以使用layout_constraintDimensionRatio 来解决我的问题。

    所以我用于显示整个布局的 xml 如下,在我的情况下,我使用 app:layout_constraintDimensionRatio="1:1.27" 和:高度比,但实际比例是 app:layout_constraintDimensionRatio="1:1.41"

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/orange">
    
        <!-- divider line which i used as restricting my A4 size container height-->
    
        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/headline_title_color"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias=".85"/>
    
        <!-- A4 size Image View-->
    
        <ImageView
            android:id="@+id/resumeContainer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="12dp"
            android:layout_marginBottom="16dp"
            android:background="@color/green"
            android:text="@string/change"
            android:src="@drawable/banner"
            app:layout_constraintBottom_toTopOf="@id/divider"
            app:layout_constraintDimensionRatio="1:1.27"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />
    
        <!-- for Bottom two buttons -->
    
        <com.bold.job.utils.CustomButton
            android:id="@+id/preview"
            style="@style/tertiaryButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:onClick="preview"
            android:text="@string/preview_resume"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/guideline"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/divider"
            />
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5" />
    
        <com.bold.job.utils.CustomButton
            android:id="@+id/preview2"
            style="@style/tertiaryButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:onClick="preview"
            app:layout_constraintLeft_toRightOf="@id/guideline"
            app:layout_constraintRight_toRightOf="parent"
            android:text="@string/preview_resume"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/divider"
            />
    
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案4】:

      请注意最后一行,在边缘添加约束会使约束起作用。

      您还可以使用 Studio 中的设计视图,并拖放对象之间的约束。

      <TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Available chats" />
      
      <ListView
          android:id="@+id/listChats"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          app:layout_constraintTop_toBottomOf="@+id/textView"/>
      

      【讨论】:

      • 您的回答如何解决问题?源代码的最后一行仅表示一个约束,但应该有更多约束才能使比率起作用!
      猜你喜欢
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多