레이아웃 다루기
어플리케이션 코드에서 사용자 인터페이스의 UI Element에 접근하려면, 아래와 같이 하면 된다.
1. 레이아웃 XML 파일에 뷰를 정의하고 새로운 고유의 ID를 추가한다.
< /res/layout/main.xml >
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
2. FindViewById 메서드를 사용하여 레이아웃 내에 사용된 뷰의 레퍼런스를 얻을 수 있다.
TextView myTextView = (TextView) findViewById(R.id.TextView01);
레이아웃이 정의된 XML 파일 자체에 접근하는 것도 가능하다.
다음은 XML 파싱을 위해 /res/layout/main.xml 레이아웃 파일을 가져오는 예이다.
XmlResourceParser myMainXml = getResources().getLayout(R.layout.main);
'Resource' 카테고리의 다른 글
| 테마(Theme) 다루기 (0) | 2022.10.02 |
|---|---|
| 스타일(Style) 다루기 (0) | 2022.10.02 |
| 리소스에 대한 참조 (0) | 2022.10.02 |
| 원본(raw) 파일 다루기 (0) | 2022.10.02 |
| XML 파일 다루기 (0) | 2022.10.02 |
댓글