创建XML用于动态创建的元素 [英] Creating an XML for Dynamically created Elements
问题描述
我要XML的 TextViews
和 EditTexts
创建的动态的。一些的博客的建议,也有一些的第三方库的,可以这样做,但我没能找到一个。我基本上创建 TextViews
&放大器; EditTexts
的动态的在我的上一个按钮code
点击。
code:
的LinearLayout linearLayout1 =(的LinearLayout)findViewById(R.id.linearLayout1);
为(中间体X = 0 X - 。1; X ++){
显示显示=((窗口管理器)getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
INT宽度= display.getWidth()/ 3;
TextView的ET1 =新的TextView(本);
et1.setBackgroundColor(color.transparent);
et1.setText(无题);
et1.setGravity(Gravity.LEFT);
等的EditText =新的EditText(本);
et.setHint(点击添加);
et.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
的LayoutParams LP1 =新的LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
的LayoutParams LP2 =新的LayoutParams(宽度,
LayoutParams.WRAP_CONTENT);
// lp1.addRule(RelativeLayout.BELOW,et1.getId());
linearLayout1.addView(ET1,LP2);
linearLayout1.addView(ET,LP2);
XML:
<滚动型
机器人:ID =@ + ID / scrollView1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_alignParentTop =真
机器人:layout_toRightOf =@ + ID / addImage>
<的LinearLayout
机器人:ID =@ + ID / linearLayout1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
< / LinearLayout中>
< /滚动型>
<按钮
机器人:ID =@ + ID / addEdit
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:layout_toLeftOf =@ + ID / scrollView1
机器人:文本=编辑/>
现在的问题是,我怎么达到 XML
为的TextView
&放大器; 的EditText
为字符串
价值?难道我给他们标签
和 IDS
的静态的在 code 的或者是有什么其他的办法吗?
解决方法: 的XmlSerializer
是问题的答案。白忙一场。 :)
公共静态最终无效writeMapXml(地图VAL,字符串名称,XmlSerializer的了)
抛出XmlPullParserException,产生java.io.IOException
{
如果(VAL == NULL){
out.startTag(NULL,TextView的);
out.endTag(NULL,TextView的);
返回;
}
集合S = val.entrySet();
迭代器I = s.iterator();
out.startTag(NULL,TextView的);
如果(名字!= NULL){
out.attribute(空,姓名,TextView的);
}
out.endTag(NULL,TextView的);
}
的XMLSerializer 由机器人提供的本身就足以使动态XMLS。
I want XML for the TextViews
and EditTexts
created dynamically. Some blogs suggest that there are some Third Party Libraries that can do that but I wasn't able to find one. I am basically creating TextViews
& EditTexts
dynamically in my code on a button
click.
Code:
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" />
The question is, how do I achieve the XML
for the TextView
& EditText
as a String
value? Do I give them tags
and ids
statically in the code or is there any other way?
Solution: XmlSerializer
is the answer to the question. Thanks for nothing. :)
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");
}
XMLSerializer provided by android itself is enough to make dynamic XMLs.
这篇关于创建XML用于动态创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!