小编Tim*_*Tim的帖子

ConstraintLayout 相对于 ImageView 尺寸

我目前正在开发一个小型 Android 应用程序并使用新的 ConstraintLayout。

我有一个 ImageView,它包含一个矢量图形图像,它应该根据其纵横比占用最大的可用空间。我让它与以下代码一起工作:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="@dimen/margin"
    android:src="@drawable/image"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />
Run Code Online (Sandbox Code Playgroud)

现在,我想在确切位置放置多个自定义视图(按钮)。使用约束指南,我想出了以下内容:

<android.support.constraint.Guideline
    android:id="@+id/guideline_x"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.20"
    tools:layout_editor_absoluteX="..."
    tools:layout_editor_absoluteY="..."
    />

<android.support.constraint.Guideline
    android:id="@+id/guideline_y"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.20"
    tools:layout_editor_absoluteX="..."
    tools:layout_editor_absoluteY="..."
    />

<com.example.android.myCustomView
    android:id="@+id/myCustomView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="doSomething"
    app:layout_constraintLeft_toLeftOf="@+id/guideline_x"
    app:layout_constraintTop_toTopOf="@+id/guideline_y"
    />
Run Code Online (Sandbox Code Playgroud)

这对于我最初测试的特定设备非常有用。但是:一旦设备尺寸发生变化,自定义视图就会放置在错误的位置。

我正在寻找一种方法来放置一个自定义视图 n% 相对于图像视图的 x 坐标。我已经尝试添加app:layout_constraintLeft_toLeftOf="@+id/imageView"到指南中,但这没有任何改变。您对我如何解决此问题有任何想法吗?非常感谢!

编辑:这里有 2 张图片说明了这个问题。

三星 Galaxy S8: 三星盖乐世S8

谷歌像素 XL: 谷歌像素 XL

小红方块应始终位于相对于 Android 图标完全相同的位置。

android vector-graphics android-custom-view android-constraintlayout

1
推荐指数
1
解决办法
6134
查看次数