简体   繁体   中英

How to add more EditText in xml layout at run time in Android?

I am making an android app which on pressing add button adds 2 TextViews at the bottom of other TextViews. I have made a layout on xml. Now the problem is how to point out that layout in java part and then adding TextViews at the run time.

This answer assumed that you are using LinearLayout as a layout...!

Use this code in your activity,

LinearLayout layout = (LinearLayout) findViewById(R.id.your_layout_id);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
   LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
layout.addView(tv);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM