Android,如何在图像上的特定点对齐文本?

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

我正在学习android编程并为自己设置一个任务来掌握布局。我正在尝试创建一个虚拟的“魔术8球”。

我的目标是在白色圆圈中出现一些随机文本,通常会出现8。

如何对齐文本,以便在不同的设备上文本总是出现在与黑球相同的相对位置,所以总是在白色圆圈内?

这是我想要做的enter image description here的一个例子

到目前为止,这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical|center_horizontal"
    tools:context="uk.co.simoncarr.magic8ball.MainActivity">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="49dp"
        android:layout_centerHorizontal="true"
        android:text="@string/title"
        android:textSize="36sp"
        tools:layout_editor_absoluteX="107dp"
        tools:layout_editor_absoluteY="10dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/img8Ball"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:foregroundGravity="center_vertical|center_horizontal"
            app:srcCompat="@drawable/magic8ball"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="0dp" />

        <TextView
            android:id="@+id/txtMessage"
            android:layout_width="130dp"
            android:layout_height="wrap_content"

            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="111dp"
            android:layout_marginRight="111dp"
            android:layout_marginTop="296dp"
            android:text="The Anser is Yes"
            android:textAlignment="center"
            android:textSize="18sp"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="447dp" />

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>
android android-relativelayout
1个回答
1
投票

在回答这个问题之前,我应该评论一下这样一个事实,即ConstraintLayout中的元素不受任何限制,因此当你运行应用程序时,它们不会保留你在布局编辑器中给出它们的位置。你应该在做其他事情之前解决这个问题。

您可以看到的一种方法可能是拥有2个圆形绘图,一个具有黑色背景,一个具有白色背景,并将一个放在另一个之上。这样,您可以使用ConstraintLayout中相对较新的“循环定位”功能,如this帖子中所述。这将为您提供相对于黑色选择白色圆圈定位的灵活性和强大功能。

© www.soinside.com 2019 - 2024. All rights reserved.