XML을 TextViews
및 EditTexts
에 대해 을 동적으로 생성하려면을 사용하십시오. 일부 블로그은 타사 라이브러리이 있지만 그 중 하나를 찾을 수 없다고 제안합니다. 나는 기본적으로 TextViews
& EditTexts
을 코드에서 동적으로 생성하고 있습니다.동적으로 생성 된 요소에 대한 XML 작성
코드 :
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 : 문제는, 어떻게이 String
값으로 TextView
& EditText
에 대한 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" />
? 나는 그들에게 tags
과 ids
을 정적으로코드으로 주거나 다른 방법이 있습니까?
해결책 :XmlSerializer
이 질문에 대한 답입니다. 고마워. :)
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이란 무엇입니까? –
'오류'가 없습니다. 나는 동적으로 생성 된'TextView'와'EditText'에 대해'XML'을 생성하려고합니다. 그것을 성취하는 방법에 대해 묻습니다. –
당신은 당신이하고 싶은 것을 말했지 만 왜 그런 것은 아닙니다. 접근 방식이 잘못되었을 수 있습니다. – Simon