我对这个问题进行了大量研究,但没有得到我想要的答案。 所以我确实有一个应用程序从服务器获取字符串。该系列字符串为XML格式。
这是我将从服务器获得的示例(如您所见,它是一个布局):
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/number_ref" />
<EditText
android:id="@+id/etTxtNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/subject_ref" />
<EditText
android:id="@+id/etTxtSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/body_ref" />
<EditText
android:id="@+id/etTxtBody"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnTxtSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save_ref"/>
<Button
android:id="@+id/btnTxtSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send_ref"/>
<Button
android:id="@+id/btnTxtClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close_ref"/>
</LinearLayout>
</LinearLayout>
这将根据将从服务器上传的 xml 文件进行更改。我的问题是如何将这些字符串系列实现到布局中并作为 Activity 的布局加载。我正在考虑将它保存在一个 xml 文件中到设备的 sdcard 并将其作为布局加载,但我想在运行它之后不可能重新编译代码。有什么建议么?谢谢。
最佳答案
如 Android documentation 中所述,布局 xml 文件不在运行时使用 - 它们在编译时编译为二进制代码。因此,您不能直接将 xml 作为布局加载。
您必须编写代码来读取 xml、解释它并使用 Android api 方法创建相应的 View 。
关于java - 从服务器加载 XML 布局 (android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22239639/