繁体   English   中英

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

[英]How to place a button at an specific coordinate in an imageview

我在imageview添加了以下图像。 这是jpeg图片

在此处输入图片说明

设计器向我提供了此imageView dot的坐标。 我需要做的是在该dot上添加一个按钮。

有什么办法可以在android中实现。

您在约束布局的帮助下获得了视图,我做了同样的事情,只是检查此代码。

<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>

在此处输入图片说明

它为我提供了类似搜寻栏中间的按钮的视图

注意:请使用拖放功能,它提供了完美的实现

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

在按钮上使用setX和setY将其放置在所需位置

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

但是要使按钮在圆点上居中

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

您应该改用RelativeLayout

例:

假设您希望位置(70x80)时在屏幕上显示大小为50x60的ImageView

// 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM