如何以编程方式添加EditText?

时间:2014-06-28 10:38:37

标签: java android eclipse android-edittext

如何以编程方式添加 EditText

这是我写的:

View RV = inflater.inflate(R.layout.fragment__dummy,container, false);

LinearLayout LL = new LinearLayout(getActivity());
EditText ET = new EditText(getActivity());
ET.setId(5);
ET.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
LL.addView(ET);

return RV;

谢谢!

2 个答案:

答案 0 :(得分:2)

LinearLayout layout=(LinearLayout)view.findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
                        android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);

                EditText edttext= new EditText(this);
                edttext.setId("edittext");
                edttext.setLayoutParams(params);

            layout.addView(edttext);

答案 1 :(得分:0)

您也在动态创建LinearLayout LL

您需要将其添加到RV

((ViewGroup)RV).addView(LL);

此外,您还应为LayoutParams设置LL