如何在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=“center”将文本视图居中
代码如下
<TextView
android:text="@string/yourText"
android layout_height="wrap_content"
android layout_height="wrap_content"
android:gravity="center" />
如果您正在使用RelativeLayout,请尝试在TextView标记中使用此属性:
android:layout_centerInParent= true
简单地,在XML文件中,将文本视图重心设置为中心:
<TextView
android:gravity="center" />
使用android:gravity=“center”作为父对象
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello world"
/>
</LinearLayout>
如果使用TableLayout,请确保将TableRows的重心也设置为中心。否则它将无法工作。至少在我将TableRow的重心设置为中心之前,它没有对我起作用。
例如,如下所示:
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center">
<TextView android:text="@string/chf" android:id="@+id/tv_chf" android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"></TextView>
</TableRow>
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件