如何在安卓系统中将TextView写在ImageView的特定位置上?

4

我尝试将TextView和ImageView组合在一起,并希望将TextView放置在图像中两个圆的中心,但是我没有找到适用于不同设备的解决方案。

这是我正在使用的图像:

输入图像描述

This is what I've tried till now. I've put both the imageview and the textview in a framelayout and then tried to adjust the textview by using linearlayouts and layout weights. `

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="80">

    <ImageView
        android:id="@+id/home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/circle_pic"
        android:adjustViewBounds="true" />

    <LinearLayout
        android:id="@+id/home_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="60"
            android:gravity="center">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="85"
                android:orientation="vertical"
                android:gravity="center">

                <TextView
                    android:id="@+id/tv1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:textColor="@color/white"
                    android:text="@string/text1"/>

                <TextView
                    android:id="@+id/tv2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="50sp"
                    android:textColor="@color/white"
                    android:text="@string/text2"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="15">

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@id/text3"
            android:orientation="horizontal"
            android:layout_weight="40"
            android:gravity="center_horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="40">

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="60"
                android:orientation="vertical"
                android:gravity="center">

                <TextView
                    android:id="@+id/tv3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15sp"
                    android:text="@string/tv3"
                    android:textColor="@color/white"/>

                <TextView
                    android:id="@+id/tv4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="50sp"
                    android:text="000"
                    android:textColor="@color/white"/>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</FrameLayout>`


使用RelativeLayout将您的图像和TextView放入其中,并使用IDE的拖放功能将其放置在所需位置。 - Apurva
这是 wrap_content 和 match_parent 等属性不起作用的情况,尝试使用硬编码的值,如将图片设置为 50 dp,然后将 TextView 定位到 25 dp 以获得所需的结果。 - Illegal Argument
我已经尝试过这些了。它们不能解决跨设备功能的问题。 - Raghav Kukreja
嗨@RaghavKukreja,你找到任何解决方案了吗? - Bugs Happen
1个回答

0
你可以使用RelativeLayout来实现这个功能。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="<your_drawable>"/>
    <TextView 
          android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/imageView"
        android:layout_alignRight="@id/imageView"
        android:layout_alignTop="@id/imageView"
        android:layout_alignBottom="@id/imageView"
        android:gravity="center"/>
</RelativeLayout>

这将把TextView定位在完整图像的中心。我正在尝试将两个TextView分别定位在两个圆的中心。 - Raghav Kukreja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接