Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android text查看相对位置中的水平中心_Android_Android Layout - Fatal编程技术网

Android text查看相对位置中的水平中心

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代码

<?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" />