android - 为动态创建的元素创建 XML

标签 android xml dynamically-generated

我想要动态创建的 TextViewsEditTexts 的 XML。一些博客建议有一些第三方库可以做到这一点,但我找不到。我基本上是在点击按钮时在我的代码中动态创建TextViewsEditTexts

代码:

      LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
        for (int x = 0; x < 1; x++) {
            Display display = ((WindowManager) getApplicationContext()
                    .getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay();
            int width = display.getWidth() / 3;

            TextView et1 = new TextView(this);
            et1.setBackgroundColor(color.transparent);
            et1.setText("Untitled");
            et1.setGravity(Gravity.LEFT);
            EditText et = new EditText(this);
            et.setHint("Click to add");
            et.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);

            LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            LayoutParams lp2 = new LayoutParams(width,
                    LayoutParams.WRAP_CONTENT);
            // lp1.addRule(RelativeLayout.BELOW, et1.getId());

            linearLayout1.addView(et1, lp2);
            linearLayout1.addView(et, lp2);

XML:

    <ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/addImage" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

<Button
    android:id="@+id/addEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/scrollView1"
    android:text="Edit" />

问题是,如何将 TextViewEditTextXML 实现为 String 值?我是否在代码静态给它们标签ids还是有其他方式?

  public static final void writeMapXml(Map val, String name, XmlSerializer out)
  throws XmlPullParserException, java.io.IOException
{
    if (val == null) {
        out.startTag(null, "TextView");
        out.endTag(null, "TextView");
        return;
    }

    Set s = val.entrySet();
    Iterator i = s.iterator();

    out.startTag(null, "TextView");
    if (name != null) {
        out.attribute(null, "name", "TextView");
    }

    out.endTag(null, "TextView");
}

最佳答案

android本身提供的

XMLSerializer足以制作动态XML。

关于android - 为动态创建的元素创建 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25636421/

相关文章:

java - JBoss 7.1.1中weblogic的Application.xml文件

xml - Magento 通过 xml 将规范链接添加到头部部分

Java 和 XML (JAXP) - 缓存和线程安全怎么样?

Android:如何将生成的临时图像附加到电子邮件中?

Android:如何配置 RelativeLayout 以填充整个高度

android - 假来电Android

android - 如何在TextView中的文本末尾显示光标?

android - 为什么opengl shader mix函数让两张透明png图片变黑了?

javascript - 在 React 中动态生成的链接之外的 html 链接之间添加项目符号 (•)

c# - 动态创建/加载用户控件只能工作一次!