반응형
부분화면을 Activity에 붙이는 코드가 헷갈려서 정리 해본다. 기본적으로 XML 에 있는 데이터를 JAVA 코드에서 사용하려면 Inflation 이라는 메모리화 작업이 필요하다는것을 이해했다. 하지만 Fragment에서 inflation 할때와 Activity에서 Inflation 할때 약간의 코드가 다르다. 다음 코드를 보면 명확히 기억 할수 있을것이다.
1) XML 파일
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
2) Activity 에서 inflation 하는 코드
public class MainActivity extends AppCompatActivity {
FrameLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
container = (FrameLayout) findViewById( R.id.container );
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
inflater.inflate( R.layout.tellist, container, true );
}
}
다음과 같이 부분화면 붙일곳 에서 Inflation 해주면 된다.
'안드로이드 프로그래밍[JAVA Code] > Fragment' 카테고리의 다른 글
Fragment : Adapter 에서 View 에 대한 정리 (0) | 2019.12.12 |
---|---|
Fragment : 생명주기 (0) | 2019.12.03 |
Fragment : 프래그먼트 부분 화면 이동하기 (0) | 2019.12.02 |