java从服务器加载XML布局(安卓)
我对这个问题做了很多研究,但没有得到我想要的答案。 所以我有一个从服务器获取字符串的应用程序。字符串序列是XML格式的
下面是一个我将从服务器获得的示例(如您所见,这是一个布局):
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" >
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/number_ref" />
<EditText
安卓:id="@+id/etTxtNumber"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/subject_ref" />
<EditText
安卓:id="@+id/etTxtSubject"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/body_ref" />
<EditText
安卓:id="@+id/etTxtBody"
安卓:layout_width="match_parent"
安卓:layout_height="200dp" />
<LinearLayout
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal" >
<Button
安卓:id="@+id/btnTxtSave"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/save_ref"/>
<Button
安卓:id="@+id/btnTxtSend"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/send_ref"/>
<Button
安卓:id="@+id/btnTxtClose"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/close_ref"/>
</LinearLayout>
</LinearLayout>
这将根据从服务器上载的xml文件进行更改。我的问题是如何将这些字符串序列实现到布局中,并作为活动的布局加载。我想把它保存在一个xml文件中,放到设备的SD卡上,然后作为布局加载,但我想在运行它之后,不可能重新编译代码。有什么建议吗?谢谢
# 1 楼答案
我认为现在可以使用android中的动态布局来完成
https://www.javacodegeeks.com/2012/09/android-dynamic-and-xml-layout.html#:~:text=Android%20activity%20contains%20various%20user,activity%20in%20two%20different%20ways.&text=Basically%2C%20Layout%20file%20is%20xml,file%20present%20in%20res%2Flayout.
https://medium.com/mindorks/creating-dynamic-layouts-in-android-d4008b72f2d
# 2 楼答案
正如Android documentation中所解释的,布局xml文件在运行时不被使用——它们在编译时被编译成二进制代码。因此,不能直接将xml作为布局加载
您必须编写代码来读取xml,解释它,并使用Android api方法创建相应的视图
# 3 楼答案
常见用例 如果您需要A/B测试不同的UI设计,而无需重新上传APK。 如果应用程序的UI根据不同的用户或场景动态更改。 如果需要快速、实时地部署UI修复程序。 json2view可以帮你