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

我对此问题进行了大量研究,但没有得到想要的答案。
所以我确实有一个应用程序从服务器获取字符串。字符串系列采用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并作为布局加载,但我想在运行它之后无法重新编译代码。有什么建议?谢谢。

参考方案

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

您必须编写读取xml的代码,对其进行解释,并使用Android api方法创建相应的View。

在Android Studio中设计可滚动内容时应遵循的方法是什么? - java

我正在约束布局中设计我的应用程序,并将其放置在“滚动视图”下。我想放置更多的Card Views,但是在xml文件的预览中没有放置空间。当布局已满时,我应该在哪里拖放Buttons,TextViews等?我已经处理了文本的滚动视图,但这是另外一回事。我知道我只能输入代码,但是当内容超出定义的屏幕尺寸时,我将无法看到我正在设计的内容时,我将很难这样做。(如果您…

如何在ScrollView和LinearLayout中居中 - java

请看下面的代码<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match…

Android:如何为wrap_content设置高度动画? - java

我需要使用ValueAnimator来使用户拖动特定视图时出现自定义“放置字段”。 (我想将字段从gone, height = 0更改为visible, height = wrap_content)。我已经尝试过以下问题的解决方案:How to animate to wrap_content?当我在单个TextView上使用它时,答案就起作用了,但是当我尝试…

为什么我的应用在启动时总是崩溃-Android - java

刚刚遵循了有关如何制作按钮和活动的youtube指南。我按照他的代码减小字体大小,并且在启动时一直崩溃。有人知道为什么吗?public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { su…

android-android studio模拟器中的SSL问题,在手机上可以正常工作 - java

我有一个可以通过https调用网络服务的应用程序。当我在手机上运行apk时,效果很好。但是,在模拟器中,所有通过POST的SSL请求均失败,并显示以下信息: 读取错误:ssl = 0xb402be00:SSL库失败,通常是一个协议 错误 错误:100c50bf:SSL例程:ssl3_read_bytes:NO_RENEGOTIATION(外部/无聊的sl /…