안드로이드 개발 질문/답변
(글 수 45,052)
setContentView(사용중);
Button b = new Button(this); 이렇게 해서 버튼을 생성하고 싶은데
이미 setContentView(); 이놈을 한번사용중이라
xml을 이용할 수도 없고
Button b = new Button(this); 이렇게 객체를 생성해서
setContentView(b); 이렇게 넣을수도 없는상황인데
버튼위젯을 만들수 있는 방법있을까요 ;;
2010.10.05 15:04:14
button.xml 레이아웃을 만들고
RelativeLayout practiceLayout = new RelativeLayout(this);
practiceLayout.addView((RelativeLayout)View.inflate(this, R.layout.practice, null));
practiceLayout.addView((Button)View.inflate(this, R.layout.button, null));
setContentView(practiceLayout);
요런 식으로 처리하면 돼요.
2010.10.06 09:16:26
RelativeLayout practiceLayout = new RelativeLayout(this);
practiceLayout.addView((RelativeLayout)View.inflate(this, R.layout.practice, null));
setContentView(practiceLayout);
practiceLayout.addView((Button)View.inflate(this, R.layout.button, null));
이렇게 하셔도 버튼은 보여요.
이미 practiceLayout이 액티비티에서 보이고 있고, 그 레이아웃에 뷰를 추가한 것 뿐이니까요.
갱신은 자동으로 됩니다.




궂이 자바로만 만드셔야 한다면
LinearLayout linear = new LinearLayout(this);
setContentView(linear);
Button b = new Button(this);
b.setText("이름");
클릭리스너 만드시고.
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear.addView(b, linearParams);
대충 이런 형식으로..
글구 그냥 단순히 버튼을 만드실 거면
xml써서 Button bt = (Button) findViewById(R.id.button);
이런식으로 하시는게 더 좋을 겁니다.