简体   繁体   中英

TextView on top of ImageView in Android

I am trying to insert a textView on top on ImageView. Image is a circle and inside relativeLayout.

I am not able insert the TextView in the center of 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. I have been working on this past one hour?

Thanks!

Change the RelativeLayout width and height to wrap_content .

Use android:layout:centerInParent="true" in your TextView.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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