탭호스트

Posted by PeEn
2019. 10. 21. 15:38 Programing/Android

1.activity_main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        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" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/dog" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/cat" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/rabbit" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/horse" />
        </FrameLayout>

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

</TabHost>

2.MainActivity.java

public class MainActivity extends TabActivity {

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		TabHost tabHost = getTabHost();

		TabSpec tabSpec1 = tabHost.newTabSpec("TAG1").setIndicator("강아지");
		tabSpec1.setContent(R.id.imageView1);
		tabHost.addTab(tabSpec1);

		TabSpec tabSpec2 = tabHost.newTabSpec("TAG2").setIndicator("고양이");
		tabSpec2.setContent(R.id.imageView2);
		tabHost.addTab(tabSpec2); 

		TabSpec tabSpec3 = tabHost.newTabSpec("TAG3").setIndicator("토끼");
		tabSpec3.setContent(R.id.imageView3);
		tabHost.addTab(tabSpec3);

		TabSpec tabSpec4 = tabHost.newTabSpec("TAG4").setIndicator("말");
		tabSpec4.setContent(R.id.imageView4);
		tabHost.addTab(tabSpec4);
		
		tabHost.setCurrentTab(0);

	}

    
}

 

 

'Programing > Android' 카테고리의 다른 글

커스텀 토스트, 커스텀 다이얼로그  (0) 2019.10.23
배열과 테이블 위젯 이용한 계산기, 숫자버튼 예제  (0) 2019.10.21
날짜 시간 위젯  (0) 2019.10.21
ContextMenu  (0) 2019.10.21
Android LineChart  (0) 2019.10.16