Android XML:在RelativeLayout中居中文本视图

时间:2012-10-14 20:18:00

标签: 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" 
    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>

位于main_loading.xml中,TextView应该明确居中,并且在elicipse XML预览中,它是......(所以一切都很好,现在:))

但是:

我正在使用这个xml,所以我正在使用:

    RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.llid);
    mainLayout.removeAllViews();
    LayoutInflater inflater = getLayoutInflater();
    mainLayout.addView(inflater.inflate(R.layout.main_loading, null));

要在xml&#39; s之间进行交换,除了Textview的居中之外,它们在模拟器上完全正常工作...(它保持未对齐 - 左侧)为什么这样?如何使用上面的inflater / 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="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>

0 个答案:

没有答案