通过背景图像视图的文本视图位置

问题描述 投票:-2回答:2

我有背景imageview(带有3个红色圆圈的绿色)和3个textview(A,B,C)。如何将textviews精确放置在支持多屏设备(人像/风景)的红色圆圈的中心?

enter image description here

已编辑:也许我问错了问题。

我的主要问题是:

1-我在布局中有这样的图像,例如:

enter image description here

2-我想在图像上的正确位置放置textview

enter image description here

当我在不同的屏幕尺寸上进行测试时,文本视图的位置已更改。

enter image description here

android layout textview imageview
2个回答
1
投票

尝试一下

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">


<TextView
    android:id="@+id/num_txt"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="20dp"
    android:background="@drawable/bg_red"
    android:gravity="center"
    android:text="A"
    android:textColor="#000000"
    android:textSize="20dp" />

<TextView
    android:id="@+id/num_txt2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="100dp"
    android:background="@drawable/bg_red"
    android:gravity="center"
    android:text="B"
    android:textColor="#000000"
    android:textSize="20dp" />
<TextView
    android:id="@+id/num_txt3"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="50dp"
    android:background="@drawable/bg_red"
    android:gravity="center"
    android:text="C"
    android:textColor="#000000"
    android:textSize="20dp" />

保存在可绘制的bg_red.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="oval">
        <corners android:radius="10dip"/>
        <stroke android:color="#FF0000" android:width="5dip"/>
        <solid android:color="#FF0000"/>
    </shape>

The Output是:

enter image description here


0
投票

在文本视图中的xml属性

android:gravity="center"

并将圆形xml文件的宽度设置为变形的内容

width = "wrap_content"
© www.soinside.com 2019 - 2024. All rights reserved.