如何在TextView中水平和垂直居中文本,以便它正好出现在Android中的TextView的中间?
当前回答
如何将视图或其内容居中,例如TextView或Button水平和垂直?layout_gravity可用于定位视图位于其父视图的中心。当重力属性用于将视图的内容(例如“文本”)放置在视图的中心。
(1)水平居中文本视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"
android:background="#333"
android:textColor="#fff"
/>
</LinearLayout>
(2)垂直居中文本视图
android:layout_gravity=“center_vertical”不起作用!!
要使TextView垂直居中,您需要做一个技巧。将TextView放置在RelativeLayout中,然后设置TextView属性android:layout_centerInParent=“true”
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:gravity="center"
android:background="#333"
android:textColor="#fff"
/>
</RelativeLayout>
(3)水平居中文本
android:gravity=“center_horizontal”
(4)水平和垂直居中文本
android:gravity=“center”这将使文本水平居中垂直地
<文本视图android:layout_width=“fill_parent”android:layout_height=“100dp”android:text=“Hello World!”android:gravity=“中心”android:background=“#333”android:textColor=“#fff”/>
其他回答
您也可以使用以下组合:
android:gravity="left|center"
然后,如果文本视图宽度大于“fill_paint”,则文本仍将向左对齐(不居中,因为重力仅设置为“center”)。
如果TextView的高度和宽度是换行内容,则TextView中的文本始终居中。但如果TextView的宽度为match_parent,高度为match-parent或wrap_content,则必须编写以下代码:
对于RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World" />
</RelativeLayout>
对于LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World" />
</LinearLayout>
试试看:
如果使用textview height and width match_parent,则使用LinearLayout如果使用textview-height and width wrap_content,则可以将重力文本设置为居中,将文本对齐设置为水平居中,然后在LinearLayout中添加重力中心属性。
XML代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:gravity="center"
android:textColor="#000"
android:textSize="30sp"
android:text="Welcome to Android" />
</LinearLayout>
Java代码
您可以通过编程这样做:-
(textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
非常感谢。
实际上,我们可以通过排除fontPadding做得更好。
<TextView
android layout_height="wrap_content"
android layout_height="wrap_content"
android:includeFontPadding="false"
android:textAlignment="center"
/>
字体填充也会影响文本的位置,您可以通过添加includefontpadding=false来删除它
<TextView
android:includeFontPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
推荐文章
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件
- 修改抽射超时时间
- 如何通过数据从第二个活动到第一个活动时按回?——安卓
- 如何在android中获得当前前景活动上下文?
- 如何在Android中获取当前日期?
- 获取Android设备名称
- 在WebView中上传文件
- 加载HTML文件到WebView
- Android:为什么视图没有maxHeight ?
- 如何获得具有已知资源名称的资源id ?
- 在Android上将字符串转换为整数
- 为什么“System.out。”println“工作在Android?
- WebView显示err_cleartext_not_allowed尽管站点是HTTPS