android - 如何将按钮放置在图像视图中的特定坐标处
问题描述
我在imageview
. 这是一张jpeg图片
设计师为我提供了dot
this 中的坐标imageView
。我需要做的是在此添加一个按钮dot
。
有什么办法可以在android中实现这一点。
解决方案
您可以尝试使用 ConstraintLayout 并使用图像高度的百分比添加引导线,例如:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.50121653" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/guideline2" />
</androidx.constraintlayout.widget.ConstraintLayout>
推荐阅读
- javascript - 在 EventListener Vs Onlick Vs inline Functions 之间进行选择?
- java - 在大多数情况下,是什么让 Jsoup 比 HttpURLConnection 和 HttpClient 更快
- sql-server - 将大型临时表保存到 Perm 表中,SQL Server
- sql-server - SQL Server 条件连接技术
- java - 这个 lambda 语法 (Java) 有什么问题?
- r - 使用 tidyverse 将逻辑运算编程为参数
- java - 如何以编程方式更改颜色资源的颜色?
- swift - 在 swift 中使用委托将字符串从一个视图控制器发送到另一个视图控制器
- css - 将内容保持在侧边栏 Angular Material 的右侧
- r - 我该如何解决这个错误?错误:可变长度不同