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

查看:538
本文介绍了如何在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_horizontal.

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

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

and as @stealthcopter commented in java: .setGravity(Gravity.CENTER);

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

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