package com.example.android_layout;
import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast;
public class MainActivity extends Activity { LinearLayout testLayout;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
testLayout = new LinearLayout(this); testLayout.setOrientation(LinearLayout.VERTICAL); testLayout.setBackgroundColor(Color.rgb(0, 255, 0)); setContentView(testLayout, p); CreateButton(); //메소드 호출 }
public void CreateButton() { Button btn = new Button(this); btn.setText("자바코드에서 생성한 버튼"); btn.setBackgroundColor(Color.CYAN); testLayout.addView(btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) { Toast.makeText(getApplicationContext(), "코드로 생성한 버튼입니다.", 0).show(); } }); } } |