매일코딩/안드로이드
[안드로이드 기초] inflater로 main화면에서 sub 화면 열기
인생여희
2017. 8. 14. 12:30
반응형
inflater로 main화면에서 sub 화면 열기
#핵심코드
//서브 화면을 보여줄 인플레이터 객체 생성
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.sub1,container,true);
inflater.inflate(프레임레이아웃에 띄울 서브 페이지, 서브페이지를 넣을 해당 프레임레이아웃 객체 ,true);
#레이아웃 인플레이션 이해하기
xml(화면) -> 자바 소스 코드(기능) -> 실행
[먼저 레이아웃 정의 -> 메모리 로딩 -> 실행 ]행
setContentView(...) 가 메모리에 올라간 것을 실제 객체로 만들어 준다.
setContentView 메소드 보다 먼저 객체를 만들면 오류 발생
#메인화면 레이아웃
#sub화면 레이아웃
#메인화면 소스코드
#소스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class MainActivity extends AppCompatActivity { FrameLayout container; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //메모리상에 올라간 코드들 객체화 setContentView(R.layout.activity_main); //메인화면에서 sub화면 넣으려고 만들어준 container 객체 선언 container = (FrameLayout)findViewById(R.id.container); //버튼 객체 Button button =(Button)findViewById(R.id.button); //버튼 클릭 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //sub 화면을 보여줄 인플레이터 객체 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); //(sub화면, 들어갈 fraim id, true는 바로 적용하겠다) inflater.inflate(R.layout.sub1,container,true); } }); } } | cs |
반응형