7*4 2009-01-11 00:27 采纳率: 50%
浏览 575
已采纳

如何在 TextView 中水平和垂直居中显示文本?

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?

转载于:https://stackoverflow.com/questions/432037/how-do-i-center-text-horizontally-and-vertically-in-a-textview

  • 写回答

29条回答 默认 最新

  • elliott.david 2009-01-11 01:53
    关注

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

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(28条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样