【问题标题】:Load XML layout from server (android)从服务器加载 XML 布局 (android)
【发布时间】:2014-04-09 23:36:07
【问题描述】:

我对此问题进行了大量研究,但没有得到我想要的答案。 所以我确实有一个应用程序从服务器获取字符串。该系列字符串为 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 文件进行更改。我的问题是如何将这些字符串系列实现到布局中并作为活动的布局加载。我正在考虑将它保存在一个 xml 文件中到设备的 sdcard 并将其加载为布局,但我想在运行它之后重新编译代码是不可能的。有什么建议?谢谢。

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    正如Android documentation 中所解释的,布局xml 文件在运行时不使用——它们在编译时被编译为二进制代码。因此,您不能直接将 xml 作为 Layout 加载。

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

    【讨论】:

    • 有没有它的库?
    【解决方案2】:

    常见用例 如果您需要 A/B 测试不同的 UI 设计,而无需重新上传 APK。 如果您的应用的 UI 根据不同的用户或场景动态变化。 如果您需要快速、实时地部署 UI 修复。 json2view可以帮到你。

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      相关资源
      最近更新 更多