[英]make text at the centre of the circle
I wanted to draw a Text inside a Circle provided that text should be matched properly at the center of the circle. 我想在圆内绘制一个文本,条件是文本在圆的中心应正确匹配。 Actually I am not getting a general method/way to make text at the center. 实际上,我没有一种在中心位置制作文本的通用方法/方式。 Also it should not depend in whether the Text is in uppercase or lower case. 同样,它不应该取决于文本是大写还是小写。
My attempt :- 我的尝试:-
textPaint.TextAlign = Paint.Align.Center; // basic need.
After that I tried to get Ascent and Descent of the Font and get their half and moved the text below by this value. 之后,我尝试获取“字体的上升”和“下降”并获取它们的一半,然后按此值将文本移到下面。 But it is not centered :( 但这不是居中的:(
var fontAscent = -textPaint.Ascent(); // default value is negative for ascent
var fontDescent = textPaint.Descent();
note :- I am getting the circle at the center so its center point is correct. 注意:-我将圆设为中心,因此圆心正确。
Any Idea how can i do it. 任何想法我该怎么做。
I would try like 我会尝试
Add this line to your layout, 将此行添加到您的布局,
<TextView
android:layout_width = "100dp"
android:layout_height = "100dp"
android:background = "@drawable:circle"
android:text = "Lorem Ipsum"
android:gravity = "center" />
Then create a shape to your drawable folder. 然后为您的可绘制文件夹创建一个形状。
<shape shape="circle" stroke_width = "1dp" stroke_color = "@android:color:black" />
There should be some typo bugs. 应该有一些错字错误。 I am writing it without IDE. 我在没有IDE的情况下编写它。
Good luck there. 祝你好运。
I have similar views in my app, monas. 我的应用程式中有类似的观点,monas。 And what I have done is in the layout. 我所做的就是在布局中。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.huteri.monas"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CircleView
android:id="@+id/piegraph"
android:layout_width="330dp"
android:layout_height="330dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/sumpie_cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:fontFamily="sans-serif-condensed"
android:paddingBottom="50dp"
android:textAlignment="center" />
<TextView
android:id="@+id/sumpie_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:textAlignment="center"
android:textSize="30sp"
app:typeface="roboto_black" />
<TextView
android:id="@+id/sumpie_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:paddingTop="50dp"
android:textAlignment="center"
app:typeface="roboto_slab_light" />
</RelativeLayout>
Well, I have to admit this is not the best solution yet, but it works for me. 好吧,我不得不承认这不是最好的解决方案,但是它对我有用。 You may try it in the layout instead 您可以在布局中尝试
try this . 尝试这个 。
create a file in drawable folder as circle.xml and copy this code 在drawable文件夹中创建一个文件circle.xml并复制此代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:angle="270"
android:endColor="#00FFFFFF"
android:startColor="#00FFFFFF" />
</shape>
and for your TextView
set Background
use this android:background="@drawable/circle"
并为您的TextView
设置Background
使用此android:background="@drawable/circle"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.