android - 相对于不规则的图像视图形状放置按钮
问题描述
我需要将按钮(绿色)放在图像(红色)上显示的位置。对于纵向模式下的所有类型的手机屏幕,它应该是相同的。
这是屏幕的中间,但高度会根据屏幕大小而变化。这些是按钮的属性。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" />
想要的结果
解决方案
解决此问题的方法之一是将不规则形状分成两个形状,然后使用 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>
推荐阅读
- javascript - Firebase 错误 Firebase 存储:索引 0 处的“put”中的参数无效:预期的 Blob 或文件
- visual-studio-code - 无法解析时如何将包添加到.Net Core 控制台应用程序?
- php - Azure AD SSO = AADSTS50020 - 多租户,如何允许外部租户注册到我的应用程序?
- py2app - 在 hello word 程序上使用官方 py2app 指令使 python2.7 文件可执行时出错
- c# - 如何使用 Xamarin.Forms 的媒体插件在共享代码中实现 iOS 覆盖?
- java - 如何将流减少(计数)应用于 IntStream 并将返回类型更改为“int”
- scala - 如何编写 Kafka RestProxy Server/Client 以供生产使用
- powershell - 从 TFS 发布日志中隐藏 Import-Module 输出
- javascript - Android Studio 中的谷歌地图渲染问题
- python - Python3 作为默认的 Python 版本