如何在TextView中水平和垂直居中文本,以便它正好出现在Android中的TextView的中间?
当前回答
您可以使用android:gravity=“center”将文本视图居中
代码如下
<TextView
android:text="@string/yourText"
android layout_height="wrap_content"
android layout_height="wrap_content"
android:gravity="center" />
其他回答
试试看:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
在任何视图的ConstraintLayout中:
(使用约束布局时,将高度和宽度设置为0,并让约束管理视图边界。)
<View
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
您可以使用android:gravity=“center”将文本视图居中
代码如下
<TextView
android:text="@string/yourText"
android layout_height="wrap_content"
android layout_height="wrap_content"
android:gravity="center" />
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO"
android:textColor="@color/colorPrimaryDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
字体填充也会影响文本的位置,您可以通过添加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