首页 > 解决方案 > 相对于不规则的图像视图形状放置按钮

问题描述

我需要将按钮(绿色)放在图像(红色)上显示的位置。对于纵向模式下的所有类型的手机屏幕,它应该是相同的。

这是屏幕的中间,但高度会根据屏幕大小而变化。这些是按钮的属性。android:layout_marginBottom并非适用于所有屏幕尺寸。

按钮

<ImageView
    android:id="@+id/circle_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="100dp"
    app:srcCompat="@drawable/circle" />

不规则形状

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:adjustViewBounds="true"
    android:src="@drawable/irregular_shape" />

想要的结果

在此处输入图像描述

标签: androidandroid-launcher

解决方案


解决此问题的方法之一是将不规则形状分成两个形状,然后使用 ConstraintLayout 将按钮放置在形状之间,如下所示:

形状图像

代码将是这样的:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/shape2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/shape_bank_bill_dotted_transparent"
        app:layout_constraintBottom_toTopOf="@+id/shape1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/shape1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/shape1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/shape_bank_bill_dotted_transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

推荐阅读