【问题标题】:How to place a button at an specific coordinate in an imageview如何将按钮放置在图像视图中的特定坐标处
【发布时间】:2019-07-10 07:21:53
【问题描述】:

我在imageview 中添加了以下图片。这是一张jpeg图片

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

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

【问题讨论】:

  • 你可以使用ConstraintLayout
  • 创建一个大小与您的图像相同的 FrameLayout 怎么样。在里面首先放置你的 ImageView(匹配父大小),然后是 Button。然后只需在 Button 上设置边距。
  • 你想达到什么目的?如果您只希望图像的特定区域可点击,则按钮可能不是最好的。

标签: android android-layout android-imageview


【解决方案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>
    

    它给了我在搜索栏中间的类似按钮的视图

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

    【讨论】:

      【解决方案3】:

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

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

      但是要让按钮在点上居中

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

      【讨论】:

        【解决方案4】:

        你应该改用RelativeLayout

        示例:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-19
          • 2022-09-19
          • 1970-01-01
          相关资源
          最近更新 更多