main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >
            </FrameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </TabHost>
</LinearLayout>

tabhost의 id가 @android:id/tabhost가 아닌 @+id/tabhost 이다.

위 tab은 FrameLayout 속성에 layout_weight="1" 때문에 하단에 위치하게 된다.


MainActivity.java

public class MainActivity extends ActivityGroup {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		createTab();
	}

	private void createTab() {
		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup(getLocalActivityManager());

		tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("TAB1")
				.setContent(new Intent(this, Tab1Activity.class)));
		tabHost.addTab(tabHost.newTabSpec("TAB2").setIndicator("TAB2")
				.setContent(new Intent(this, Tab2Activity.class)));

		}

	}
}


참고 사이트



 실행환경

 Notebook

 SAMSUNG NT550p5c-s61r

 CPU

 Intel Core i5-3210M 2.50GHz

 Memory

 8 GB

 OS

 Window 7 ultimate 64bit

 Java

 1.7.0_51

 Android SDK : 4.4.2 (KitKat) / 테스트기기 : Galaxy S3 4.3 (Jelly Bean)

 WebServer

 Apache Tomcat 7.0


TabHost

여러 탭을 두고 각 탭을 클릭할 때마다 해당 화면이 나오도록 설정하는 뷰 컨테이너.

예제 결과

XML code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#00ff00"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="1" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#0000ff"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="2" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#ff0000"
                    android:orientation="vertical" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="3" />
                </LinearLayout>
            </FrameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
        </LinearLayout>
    </TabHost>

</LinearLayout>

java code

package com.android_report;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TabHost tabHost = getTabHost();

		TabSpec tabSpecTab1 = tabHost.newTabSpec("TAB1").setIndicator("강아지");
		tabSpecTab1.setContent(R.id.tab1);
		tabHost.addTab(tabSpecTab1);

		TabSpec tabSpecTab2 = tabHost.newTabSpec("TAB2").setIndicator("고양이");
		tabSpecTab2.setContent(R.id.tab2);
		tabHost.addTab(tabSpecTab2);

		TabSpec tabSpecTab3 = tabHost.newTabSpec("TAB3").setIndicator("토끼");
		tabSpecTab3.setContent(R.id.tab3);
		tabHost.addTab(tabSpecTab3);

		TabSpec tabSpecTab4 = tabHost.newTabSpec("TAB4").setIndicator("말");
		tabSpecTab4.setContent(R.id.tab1);
		tabHost.addTab(tabSpecTab4);
		
		tabHost.setCurrentTab(0);

	}
}


+ Recent posts