본문 바로가기

Android

레이아웃 관리 - 전개(inflation) 실습

Text.xml 파일 생성

 

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:gravity="center"

    android:text="TextView"

    android:textColor="#ff0000"

    android:textSize="20sp" >

 

 

</TextView>

 

 

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.view.View;

import android.widget.LinearLayout;

import android.widget.TextView;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

 

    

 

           LinearLayout linear = new LinearLayout(this);

           linear.setOrientation(LinearLayout.VERTICAL);

           linear.setBackgroundColor(Color.LTGRAY);

 

           TextView text1 = (TextView)View.inflate(this, R.layout.text, null);

           TextView text2 = (TextView)View.inflate(this, R.layout.text, null);

 

           linear.addView(text1);

           linear.addView(text2);

           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;

     }

 

}

 TextView 하나에 대해서만 XML문서를 만들어 놓고 전개해서 사용 가능하다.