添加多个布局动态的android [英] adding multiple layout dynamically android
本文介绍了添加多个布局动态的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想点击一个按钮时动态添加文本视图。我已经实现了这一点,但在写previous one.Anything错在我的code添加的时候吗?
imGbtn.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){
INT标识= 0;
。最终的字符串数据= textView.getText()的toString();
//childHolder.title.setText(data);
LL的LinearLayout =新的LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setId(ID);
tvll.addView(Ⅱ);
// TextView的TV1 =新的TextView(mContext);
// tv1.setText(动态布局FTW!);
// ll.addView(TV1); LinearLayout.LayoutParams的LayoutParams =新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(25,20,25,10);
TextView的电视=新的TextView(mContext);
tv.setId(ID);
tv.setText(数据);
ll.addView(电视的LayoutParams);
textView.setText(); }
});
解决方案
以下code中的LinearLayout添加EDITTEXT的onclick
公共类ViewOnClick延伸活动{
LinearLayout.LayoutParams的LayoutParams;
LL的LinearLayout;
静态INT I;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
按钮B =(按钮)findViewById(R.id.Button01); b.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
LL的LinearLayout =新的LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setId(ID);
LinearLayout.LayoutParams的LayoutParams =新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(25,20,25,10);
观点的EditText =新的EditText(ViewOnClick.this);
view.setText(++ 1 +意见);
ll.addView(视图的LayoutParams); }});
}
}
I want to add text view dynamically when clicking a button . I have implemented this but when adding it over writing the previous one.Anything wrong on my code ?
imGbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id =0;
final String data = textView.getText().toString();
//childHolder.title.setText(data);
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setId(id);
tvll.addView(ll);
// TextView tv1 = new TextView(mContext);
// tv1.setText("Dynamic layouts ftw!");
// ll.addView(tv1);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(25, 20, 25, 10);
TextView tv= new TextView(mContext);
tv.setId(id);
tv.setText(data);
ll.addView(tv,layoutParams);
textView.setText("");
}
});
解决方案
following code add Edittext in linearlayout onclick
public class ViewOnClick extends Activity {
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
static int i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.Button01);
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setId(id);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(25, 20, 25, 10);
EditText view = new EditText(ViewOnClick.this);
view.setText(++i+" view");
ll.addView(view, layoutParams);
}});
}
}
这篇关于添加多个布局动态的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文