【问题标题】:Use ratio for ImageView size with ConstrainLayout for ResponsivenessImageView 大小的使用比例与 ConstrainLayout 的响应性
【发布时间】:2018-03-27 23:23:17
【问题描述】:

我正在使用 ConstraintLayout 使我的应用能够响应不同的屏幕尺寸。

我想使用比率来调整小部件的大小。 所以要使用比率,我必须将我的宽度和高度设置为 match_constraints。

所以我正在做的是:

    <ImageView
    android:id="@+id/my_img"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="150dp"
    android:layout_marginStart="250dp"
    android:layout_marginTop="5dp"
    android:adjustViewBounds="true"
    android:src="@drawable/img_test"
    app:layout_constraintBottom_toTopOf="@+id/bot_img"
    app:layout_constraintDimensionRatio="h,125:50"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/top_img" />

所以我将使用边距和比率(以 dp 为单位)来设置我的 img 大小。 问题是当我使用不同的屏幕尺寸时,我的 img 不会保持我想要的确切比例。

你能帮帮我吗?我使用 ConstraintLayout 对吗?非常感谢

【问题讨论】:

    标签: android android-studio responsive-design responsive android-constraintlayout


    【解决方案1】:

    我想你误解了constraintDimensionRatio 的用法。使用width="0dp" 设置两个水平约束以及设置约束维度比率(如 1:1)。并使用height="0dp" 仅设置一个垂直约束(如顶部)并保留另一个(或反之亦然,具体取决于您的要求)。现在您的视图的宽度将尽可能扩大,高度将根据比例进行调整。请参阅下面的示例以更好地理解。

    <ImageView
        android:id="@+id/my_img"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        android:src="@drawable/img_test"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
    

    以上代码将生成一个正方形图像,该图像将根据不同的父宽度进行缩放。

    编码愉快:)

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多