用JAVA代码为android应用添加一个按钮

      LinearLayout layout = new LinearLayout(ActivityRegisterCity.this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(0, 0, 0, 0);
        layout.setLayoutParams(new
                ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        Button btn = new Button(ActivityRegisterCity.this); 
        LinearLayout.LayoutParams params = new
                LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 0);
        btn.setLayoutParams(params);
        layout.addView(btn);
        setContentView(layout);

你可能感兴趣的:(JAVA)