如何在TextView中水平和垂直居中文本,以便它正好出现在Android中的TextView的中间?
当前回答
使用TextView中的“重力属性”垂直和水平居中文本。
android:gravity="center"
其他回答
在对TextView使用gravity时,API级别17中实现了另一种方法-
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
不知道有什么区别,但它也起作用。但仅适用于API 17级或更高级别。
试试看:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
这是我在应用程序中使用的答案。它在屏幕中央显示文本。
<TextView
android:id="@+id/txtSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/subject"
android:layout_margin="10dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
如果您试图在TableLayout中的TableRow上居中文本,以下是我实现这一点的方法:
<TableRow android:id="@+id/rowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView android:id="@+id/lblSomeLabel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="100"
android:text="Your Text Here" />
</TableRow>
将布局文件中的重力属性设置为android:gravity=“中心”
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。