본문 바로가기

Android

레이아웃 관리 - 전개자(inflater)에 의한 수행

Inflation.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"

    android:background="#cccccc">

   

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center"

        android:textColor="#ff0000"

        android:textSize="20sp"

        android:text="TextView"/>

 

</LinearLayout>

 

 

MainActivity.java

 

package com.example.testex12;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.content.Context;

import android.graphics.Color;

import android.text.Layout;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.Menu;

import android.widget.LinearLayout;

import android.widget.TextView;

 

public class MainActivity extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

 

        LayoutInflater inflater

        = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

       

    LinearLayout linear = (LinearLayout)inflater.inflate(R.layout.inflation, null);

    setContentView(linear);

       

    }

 

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

   

}

 

NEW 연산자와 속성변경 메서드 호출이 전개자에 의해 대신 수행됨