简体   繁体   English

Android中ImageView之上的TextView

[英]TextView on top of ImageView in Android

I am trying to insert a textView on top on ImageView. 我正在尝试在ImageView的顶部插入一个textView。 Image is a circle and inside relativeLayout. 图像是一个圆圈,位于relativeLayout内部。

I am not able insert the TextView in the center of Circle. 我无法将TextView插入Circle的中心。

Snapshot: 快照:

圆圈内的文字

Please take a look at the snapshot and the code. 请看一下快照和代码。

<RelativeLayout
            android:id="@+id/circlenumberlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/layoutbackground" >

            <ImageView
                android:id="@+id/numberCircle"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:padding="5dp"
                android:src="@drawable/circle"
                tools:ignore="RtlHardcoded" />

            <TextView
                android:id="@+id/numberText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/numberCircle"
                android:layout_marginRight="16dp"
                android:gravity="center"
                android:text="200"
                android:textStyle="bold" 
                android:textColor="@color/whiteText"
                android:textSize="14sp"/>

        </RelativeLayout>

Can somebody help me to get the textView centered to the imageCircle. 有人可以帮我将textView居中到imageCircle的中心。 I have been working on this past one hour? 我过去一个小时一直在工作吗?

Thanks! 谢谢!

Change the RelativeLayout width and height to wrap_content . 将RelativeLayout的宽度和高度更改为wrap_content

Use android:layout:centerInParent="true" in your TextView. 在TextView中使用android:layout:centerInParent="true"

If your requirement is just to have a TextView with a border such that the text used for TextView is always at the center then you can achieve it using below code instead of having a RelativeLayout and an ImageView . 如果您只需要具有边框的TextView以便用于TextView的文本始终位于中心,则可以使用以下代码来实现它,而不是使用RelativeLayoutImageView

<TextView
    android:id="@+id/numberText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@drawable/circle"
    android:text="200"
    android:textStyle="bold"
    android:textColor="@color/whiteText"
    android:textSize="14sp"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM