【问题标题】:Creating an XML for Dynamically created Elements为动态创建的元素创建 XML
【发布时间】:2021-04-12 14:45:54
【问题描述】:

我想要为TextViewsEditTexts 创建动态 的XML。一些 博客 建议有一些 第三方库 可以做到这一点,但我找不到。我基本上是在我的代码中通过button 点击创建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 值?我是在代码静态给他们tagsids还是有其他方法?

  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");
}

【问题讨论】:

  • @MurdocN 的错误是什么?
  • 没有error。我想为动态生成的TextViewEditText 生成XML。询问如何实现这一目标。
  • 你已经说了你想做的事,但没有说为什么。你的方法可能是错误的。
  • 为什么动态生成的视图需要XML?这样我就可以将它们作为字符串保存在数据库中,并在需要时检索它们。
  • @MurdocN 不可能使用这样的 xml,您认为@+id/scrollView1 的值是多少?

标签: android xml dynamically-generated


【解决方案1】:
android本身提供的

XMLSerializer足以制作动态的XML。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多