【问题标题】:textview position by background imageview通过背景图像视图的文本视图位置
【发布时间】:2016-06-16 11:36:56
【问题描述】:

我有背景图像视图(绿色,带有 3 个红色圆圈)和 3 个文本视图(A、B、C)。 如何将文本视图准确地放在支持多屏设备、纵向/横向的红色圆圈的中心?

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

我的主要问题是:

1- 我的布局中有一个图像,例如:

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

当我在不同的屏幕尺寸上进行测试时,textviews 的位置发生了变化。

【问题讨论】:

  • 见下面的答案。

标签: android layout textview imageview


【解决方案1】:

在文本视图xml属性中

android:gravity="center"

并将圆形 xml 文件宽度设置为扭曲内容

width = "wrap_content"

【讨论】:

    【解决方案2】:

    试试这个

    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" />
    

    保存在drawable 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>
    

    输出是:

    【讨论】:

    • 谢谢,但我的问题是:如何将 textview 放置在与背景匹配的位置并且不随屏幕大小而变化
    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多