AndroidXML:在RelativeLayout中居中文本视图

AndroidXML:在RelativeLayout中居中文本视图,android,xml,layout,Android,Xml,Layout,我有这个XML代码,我想用它作为我的加载屏幕的基础… <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" andro

我有这个XML代码,我想用它作为我的加载屏幕的基础…

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:gravity="center"
    android:id="@+id/loading">

    <TextView
        android:id="@+id/loading_message"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="LOADING..." />
</RelativeLayout>
要在xml之间交换,除了文本视图的居中,所有这些都可以在模拟器上正常工作。。。(它保持不对齐-左上角)这是为什么?如何使用上述充气机/布局代码使某物居中

非常感谢

编辑:我的另一个XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:id="@+id/llid">

<EditText android:id="@+id/edit_message"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

<Button android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="Login" 
    android:layout_toRightOf="@id/edit_message" />

</RelativeLayout>


问题似乎出在RelativeLayout
llid
上。它要么被设置为
wrap_content
,要么它的
gravity
未居中。你能发布
llid
的XML吗?我同意@Sam的观点。您可以尝试检查是否存在这种情况的另一件事是,将main_加载背景设置为与llid不同的颜色,以便查看main_加载是如何放置在其父级中的。在单个活动中交换布局对我来说似乎很奇怪。你考虑过使用片段吗?@Sam,是的,llid不居中(它是wrap_内容),虽然我想要一个居中,一个不居中,但这可以用inflater实现吗?你可以用LayoutInflater做任何事情,就像用纯XML一样,您看到的问题是,您将布局居中放置在偏移布局内。。。在没有看到更多代码的情况下,我所能建议的就是划分布局:将想要的布局居中;离开要偏移的布局时,偏移。。。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:id="@+id/llid">

<EditText android:id="@+id/edit_message"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

<Button android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="Login" 
    android:layout_toRightOf="@id/edit_message" />

</RelativeLayout>