【问题标题】:Have an ImageView with same width and height in ConstraintLayout在 ConstraintLayout 中具有相同宽度和高度的 ImageView
【发布时间】:2019-03-05 06:32:12
【问题描述】:

如何让ImageView的宽高等于父ConstraintLayout的宽度?所以它会显示一个全宽的方形 ImageView。

通常如何将某个小部件的高度设置为等于其他小部件的宽度?

【问题讨论】:

    标签: imageview android-imageview android-constraintlayout


    【解决方案1】:

    以下布局将在父ConstraintLayout 的中心放置一个方形蓝色图像。关键是app:layout_constraintDimensionRatio="1:1"。详情请参考documentation

    您还可以将小部件的一个维度定义为另一个维度的比率。为此,您需要将至少一个约束维度设置为 0dp(即 MATCH_CONSTRAINT),并将属性 layout_constraintDimensionRatio 设置为给定的比率。

    <android.support.constraint.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@android:color/holo_blue_light"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-31
      • 2018-06-02
      • 2015-11-27
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2018-06-01
      相关资源
      最近更新 更多