Как разместить кнопку в определенной координате в просмотре изображений - PullRequest
0 голосов
/ 16 февраля 2019

Я добавил следующее изображение в imageview.Это изображение в формате JPEG

enter image description here

Дизайнер предоставил мне координаты dot в этом imageView.Что мне нужно сделать, это добавить кнопку над этим dot.

Есть ли способ, которым я могу добиться этого в Android.

Ответы [ 4 ]

0 голосов
/ 16 февраля 2019

Вместо этого вы должны использовать RelativeLayout

ПРИМЕР:

Предположим, вы хотите, чтобы на вашем экране было изображение размером 50x60 в позиции (70x80)

// RelativeLayout.хотя вы также можете использовать 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);
0 голосов
/ 16 февраля 2019

Используйте setX и setY на кнопке, чтобы поместить ее туда, где вы хотите

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

Но центрировать кнопку на точке

button.setX(dotX - buttonWidth/2);
button.setY(dotY - buttonHeight/2);
0 голосов
/ 16 февраля 2019

Вы получаете свое представление с помощью макета ограничения, я сделал что-то такое же, просто проверьте этот код ....

<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

Этодает мне вид кнопки в центре панели поиска

ПРИМЕЧАНИЕ. Пожалуйста, используйте перетаскивание, чтобы получить идеальную реализацию

0 голосов
/ 16 февраля 2019

Вы можете попробовать 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>
...