Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

android - 如何在TextView中将文本水平和垂直居中?(How do I center text horizontally and vertically in a TextView?)

如何在TextView水平和垂直居中放置文本,以使其准确显示在Android TextView的中间?

  ask by pupeno translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm assuming you're using XML layout.

(我假设您正在使用XML布局。)

<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.

(您也可以根据需要使用重力center_verticalcenter_horizontal 。)

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

(就像@stealthcopter在Java中评论的那样: .setGravity(Gravity.CENTER);)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...