如何在imageview中的特定坐标处放置按钮

问题描述 投票:0回答:4

我在imageview中添加了以下图像。这是一个jpeg图像

enter image description here

设计师为我提供了dotimageView的坐标。我需要做的是在这个dot上添加一个按钮。

有什么方法可以在android中实现这一点。

android android-layout android-imageview
4个回答
1
投票

您可以尝试使用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>

2
投票

你在约束布局的帮助下得到你的视图我做了同样的事情只需检查这个代码....

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".5">

    <me.tankery.lib.circularseekbar.CircularSeekBar
        android:id="@+id/circularSeekBar"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:cs_circle_color="#0000ff"
        app:cs_circle_progress_color="#ff0000"
        app:cs_circle_stroke_width="4dp"
        app:cs_pointer_color="#ff0000"
        app:cs_pointer_stroke_width="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/play"
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#0000"
        android:src="@drawable/play"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

enter image description here

它给了我像搜索栏中间按钮的视图

注意:使用拖放操作可以实现完美的实现


1
投票

使用按钮上的setX和setY将其放在您想要的位置

button.setX(dotX);
button.setY(dotY);

但要将按钮置于圆点上

button.setX(dotX - buttonWidth/2);
button.setY(dotY - buttonHeight/2);

1
投票

您应该使用RelativeLayout

例:

假设你想要一个尺寸为50x60的ImageView(位置为70x80)

// RelativeLayout。虽然你也可以在findViewById()这里使用xml RelativeLayout

 RelativeLayout relativeLayout = new RelativeLayout(this);
        // ImageView
        ImageView imageView = new ImageView(this);

        // Setting layout params to our RelativeLayout
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50, 60);





// Setting position of our ImageView
        layoutParams.leftMargin = 70;
        layoutParams.topMargin = 80;

        // Finally Adding the imageView to RelativeLayout and its position
        relativeLayout.addView(imageView, layoutParams);
© www.soinside.com 2019 - 2024. All rights reserved.