如何在 TextView 中水平和垂直居中文本? [英] How do I center text horizontally and vertically in a TextView?

查看:42
本文介绍了如何在 TextView 中水平和垂直居中文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 TextView 中将文本水平和垂直居中,使其恰好出现在 AndroidTextView 的中间?

How do I center the text horizontally and vertically in a TextView, so that it appears exactly in the middle of the TextView in Android?

推荐答案

我假设你使用的是 XML 布局.

I'm assuming you're using XML layout.

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="@string/**yourtextstring**"
/>

您也可以根据需要使用重力center_verticalcenter_horizo​​ntal.

You can also use gravity center_vertical or center_horizontal according to your need.

正如@stealthcopter 所说,在java 中:.setGravity(Gravity.CENTER);.

As @stealthcopter commented, in java: .setGravity(Gravity.CENTER);.

对于 Kotlin 用户,.gravity = Gravity.CENTER

And for Kotlin users, .gravity = Gravity.CENTER

这篇关于如何在 TextView 中水平和垂直居中文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆