본문 바로가기
App/Android Java

Android_java(11) - TabHost

by SeleniumBindingProtein 2022. 2. 8.
728x90
반응형

탭호스트 (TabHost)
        - 여러 탭을 두고 각 탭을 클릭할 때마다 해당 화면이 나오도록 설정하는 뷰 컨테이너 
        - 탭호스트, 탭위젯, 프레임레이아웃은 지정된 id를 변경하지 않아야 안드로이드가 탭호스트의 구성임을 인식함 

package kr.co.tabhost;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends TabActivity {

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

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

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

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

        TabHost.TabSpec tabSpec4 = tabHost.newTabSpec("TAG4").setIndicator("키캣");
        tabSpec4.setContent(R.id.imageView4);
        tabHost.addTab(tabSpec4);

        tabHost.setCurrentTab(0);
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayout"
        android:orientation="vertical">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent"
            android:layout_weight="1">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView1"
                android:layout_gravity="center"
                android:src="@drawable/dog3"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView2"
                android:layout_gravity="center"
                android:src="@drawable/cat1"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView3"
                android:layout_gravity="center"
                android:src="@drawable/rabbit2"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView4"
                android:layout_gravity="center"
                android:src="@drawable/kitkat"/>
        </FrameLayout>
        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"
            android:background="#F0F000">

        </TabWidget>
    </LinearLayout>

</TabHost>

[결과 출력]

0123

 

 

 

 

 

728x90
반응형

'App > Android Java' 카테고리의 다른 글

Android_java(13) - AlertDialogApp  (0) 2022.02.09
Android_java(12) - OptionMenuApp  (0) 2022.02.09
Android_Java(10) -VeiwFlipperApp  (0) 2022.02.08
Android_Java(9) - DateTimeApp  (0) 2022.02.08
Android_Java(8) - CalcuWithTableLayout  (0) 2022.02.07

댓글