视图不会出现在屏幕上

时间:2017-05-22 21:18:11

标签: android

单击该按钮可创建视图。然后我将此视图添加到布局中,但它不会出现在屏幕上。

private ArrayList<EditText> viewArray = new ArrayList<EditText>();
String str = "text";
LinearLayout linLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    linLayout = new LinearLayout(this);
    linLayout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    setContentView(linLayout, linLayoutParam);

    Button btnAdd = new Button(this);
    btnAdd.setText("Add EditText");
    LinearLayout.LayoutParams vParams = new LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    linLayout.addView(btnAdd, vParams);

    btnAdd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            viewArray.add( new EditText(getApplicationContext()) );
            viewArray.get(viewArray.size()-1).setText(str + viewArray.size());
            viewArray.get(viewArray.size()-1).setId(viewArray.size());
            LinearLayout.LayoutParams vParams2 = new LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            linLayout.addView(viewArray.get(viewArray.size()-1), vParams2);

            Toast.makeText(getApplicationContext(),"size: " + viewArray.size(), Toast.LENGTH_LONG).show();
        }
    });
}
}

应用程序未关闭,没有错误,视图根本没有出现

0 个答案:

没有答案