LayoutInflater 의 다양한 코드
LayoutInflater 이란 놈은 xml 문서의 리소스를 받아 객체를 생성해주는 역할을 수행한다.
xml 리소스를 받는 대부분의 안드로이드 API 메서드들의 내용을 보자면 LayoutInflater 를 통해서 xml 리소스를 활용할것이다.
xml 과 연계한 커스텀뷰를 만들고자 하면 빠짐없이 등장할 클래스이며,
LayoutInflater 를 활용하는 레퍼런스 코드가 몇가지 있는데, 코드가 다르다해서 기능 또한 다르지는 않으니 모두 봐두는것이 좋겠다.
입맛대로 골라쓰면 되것지.
1. getSystemService 메서드
Activity 클래스 안에서.. {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linear = (LinearLayout)inflater.inflate( xml리소스 , null );
setContentView(linear);
}
2. LayoutInflater 의 정적메서드
Activity 클래스 안에서.. {
LayoutInflater inflater = LayoutInflater.from(this); // this => Context 타입
LinearLayout linear = (LinearLayout)inflater.inflate( xml리소스 , null );
setContentView(linear);
}
3. View 의 정적 메서드
Activity 클래스 안에서.. {
LinearLayout linear = (LinearLayout)View.inflate(this, xml리소스 , null ); // this => Context 타입
setContentView(linear);
}
※ 공통 : inflate 메서드의 마지막 인자 null 은 루트View가 이미 존재한다면 그 뷰를 넣어주면 되는것이고, 없으면 null 넣어주면 됨.
원문 : http://manhdh.blog.me/120151874115
[출처] [코드] LayoutInflater 의 다양한 코드|작성자 개구리