티스토리 뷰
안드로이드 LayoutInflater 사용하기
안드로이드에서 레이아웃 XML파일을 View객체로 만들기 위해서는 LayoutInflater를 이용해야 합니다.
사용방법은 다음과 같으며, 자세한 사항은 아래에서 알아봅시다.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test_layout, parent, false);
LayoutInflater 생성하기
1. getSystemService
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.my_layout, parent, false);
2. Activity의 getLayoutInflater() 사용
LayoutInflater inflater = getLayoutInflater();
3. LayoutInflater 팩토리 메서드
LayoutInflater inflater = LayoutInflater.from(context)
View view = inflater.inflate(R.layout.my_layout, parent, false);
4. View 팩토리 메서드
View view = View.inflate(context, R.layout.my_layout, parent);
(주의. View의 inflate는 parent를 넣어주게 되면, 자동으로 attach됩니다.)
Param 설명
inflate(int resource, ViewGroup root, boolean attachToRoot)
resource: view를 만들고 싶은 레이아웃 파일의 id,
root: attachToRoot가 true일경우 생성되는 View가 추가될 부모 뷰, attachToRoot가 false일 경우에는 LayoutParams값을 설정해주기 위한 상위 뷰, null로 설정할경우 android:layout_xxxxx값들이 무시됨.
attachToRoot: true일 경우 생성되는 뷰를 root의 자식으로 만든다, false일 경우 root는 생성되는 View의 LayoutParam을 생성하는데만 사용된다.
'Mobile > Android' 카테고리의 다른 글
| [Android]ListView안의 EditText의 Focus문제 (0) | 2015.08.10 |
|---|---|
| [Android]Account 화면 호출 (0) | 2015.08.03 |
| [Android]안드로이드 스튜디오로 빌드시 log파일 삭제 (0) | 2015.07.15 |
| [Android]gradle (0) | 2015.07.13 |
| [Android]RelativeLayout의 속성 (0) | 2015.07.06 |
- Total
- Today
- Yesterday
- php
- cocoapods
- CentOS
- docker
- intellij
- SWIFT
- MySQL
- github
- tomcat
- Xcode
- ubuntu
- rxswift
- Codable
- Linux
- ios
- Gradle
- go
- android
- windows10
- nodejs
- Java
- war
- Kotlin
- golang
- Spring
- centos8
- Windows
- enum
- Python
- git
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |