Android text查看相对位置中的水平中心
在我的应用程序屏幕中,我想将标题显示为水平居中。我尝试了以下布局xml代码Android text查看相对位置中的水平中心,android,android-layout,Android,Android Layout,在我的应用程序屏幕中,我想将标题显示为水平居中。我尝试了以下布局xml代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layo
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dip"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Connections" />
</RelativeLayout>
谢谢使用
layout\u centerInParent=“true”(layout\u centerinorizontal,layout\u centerininvertial)
gravity是指textview上文本的algin,而不是视图本身,android:gravity控制textview中的外观。因此,如果有两行文本,它们将在TextView的边界内居中。请尝试使用以下方法:
android:layout_centerHorizontal="true"
在文本视图中,仅添加此
android:gravity=“center\u horizontal”
将textview替换为以下代码
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:text="Connections" />
请参见“谢谢”,它起作用了,我认为唯一的区别是用换行符填充内容。请注意,如果RelativeLayout包含属性android:gravity=“center\u horizontal”,则此操作无效。如果此操作无效,确保文本视图的宽度也设置为wrap\u content
,而不是match\u parent
使文本视图的宽度:wrap content不可见。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dip"
>
<TextView
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Connections" />
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:text="Connections" />