java - 从服务器加载 XML 布局 (android)

标签 java android xml android-layout

我对这个问题进行了大量研究,但没有得到我想要的答案。 所以我确实有一个应用程序从服务器获取字符串。该系列字符串为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/

相关文章:

java - 当 EditText 在 ListView 中时,selectAllOnFocus 的奇怪行为

xml - CDATA 中的 XSL 转义 HTML

android - android中的自定义对象点击问题

java - Google+ 登录结果为 "unknown error"- Google Play 游戏服务

android - 使用移动数据时出现 JsonSyntaxException

php - 将 KML 转换为 GeoJson

android - 使用 Appcompat 在 pre-Lollipop 上错误的 Button textColor 和 drawablePadding

javascript - 如果 Angular 正在做......从 Selenium 中检测东西

java - 如何在 Struts with Tiles 中获取真实的请求 URL?

java - Android:由于存在软件包,即使以前没有安装过,也需要卸载