从服务器加载XML布局(android) [英] Load XML layout from server (android)

查看:92
本文介绍了从服务器加载XML布局(android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此问题进行了大量研究,但我得不到我想要的答案。
所以我确实有一个应用程序从服务器获取字符串。这一系列的字符串是XML格式。

I had lots of research about this issue but I didn't get the answer that I want. So I do have an application getting string from the server. The series of string is in XML Format.

这是我从服务器获得的一个例子(你可以看到它是一个布局):

Here is an example of what I shall get from the server (as you can see it's a layout):

<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文件而改变。我的问题是如何将这些字符串系列实现为布局并作为活动布局加载。我想将它保存在xml文件中的设备的SD卡并加载它作为布局但我想在运行它后不可能重新编译代码。有什么建议?谢谢。

This will be changed depending on what xml file will be uploaded from the server. My problem is how can I implement these series of string into layout and to be loaded as layout of an activity. I was thinking of saving it in an xml file to sdcard of the device and load it as layout but I guess it's not possible to recompile the code after running it. Any suggestions? Thank you.

推荐答案

Android文档,布局xml文件在运行时不使用 - 它们在编译时被编译为二进制代码。因此,您无法直接将xml加载为布局。

As explained in the Android documentation, the layout xml files are not used at runtime - they are compiled to binary code at compile time. So, you can't directly load the xml as a Layout.

您必须编写读取xml的代码,解释它并使用Android api方法创建相应的视图。

You'd have to write code that reads the xml, interprets it, and uses Android api methods to create a corresponding View.

这篇关于从服务器加载XML布局(android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆