본문 바로가기

Android

레이아웃 관리 - 페이지 중첩

main.xml


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

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@+id/btn1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="Page 1" />

 

        <Button

            android:id="@+id/btn2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="Page 2" />

 

        <Button

            android:id="@+id/btn3"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="3"

            android:text="Page 3" />

    </LinearLayout>

 

    <FrameLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent" >

 

        <LinearLayout

            android:id="@+id/page1"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:background="#ffff00"

            android:orientation="vertical" >

 

            <Button

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:text="Linear Layout"/>

        </LinearLayout>

 

        <RelativeLayout

            android:id="@+id/page2"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:background="#00ff00"

            android:visibility="invisible" >

           

              <Button

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:text="Relative Layout"/>

        </RelativeLayout>

 

        <TableLayout

            android:id="@+id/page3"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:background="#0000ff"

            android:visibility="invisible" >

 

            <TableRow>

 

                <TextView

                    android:text="국어"

                    android:textSize="30sp" />

 

                <TextView

                    android:text="영어"

                    android:textSize="30sp" />

 

                <TextView

                    android:text="수학"

                    android:textSize="30sp" />

            </TableRow>

 

            <TableRow>

 

                <TextView

                    android:text="88"

                    android:textSize="30sp" />

 

                <TextView

                    android:text="92"

                    android:textSize="30sp" />

 

                <TextView

                    android:text="76"

                    android:textSize="30sp" />

            </TableRow>

        </TableLayout>

    </FrameLayout>

 

</LinearLayout>

 


main.java 


package com.example.testex10;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

 

public class MainActivity extends Activity {

     View page1, page2, page3;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

 

           Button btn1 = (Button) findViewById(R.id.btn1);

           Button btn2 = (Button) findViewById(R.id.btn2);

           Button btn3 = (Button) findViewById(R.id.btn3);

          

           page1 = findViewById(R.id.page1);

           page2 = findViewById(R.id.page2);

           page3 = findViewById(R.id.page3);

 

           btn1.setOnClickListener(new View.OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     page1.setVisibility(View.VISIBLE);

                     page2.setVisibility(View.INVISIBLE);

                     page3.setVisibility(View.INVISIBLE);

                }

           });

 

           btn2.setOnClickListener(new View.OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     page1.setVisibility(View.INVISIBLE);

                     page2.setVisibility(View.VISIBLE);

                     page3.setVisibility(View.INVISIBLE);

                }

           });

 

           btn3.setOnClickListener(new View.OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     page1.setVisibility(View.INVISIBLE);

                     page2.setVisibility(View.INVISIBLE);

                     page3.setVisibility(View.VISIBLE);

                }

           });

 

     }

 

     @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;

     }

 

}


각 버튼을 누를때 프레임 내의 모든페이지를 일단 숨긴 후 버튼에 대응되는 페이지 하나만 표시한다.