출처: https://3months.tistory.com/307 [Deep Play]

안드로이드/연습

[Android] TapHost

코딩하는 랄뚜기 2021. 8. 22. 23:05
package com.example.veiw;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.ViewFlipper;
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

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

        TabHost tabHost = getTabHost();

        TabHost.TabSpec tabSpecSong = tabHost.newTabSpec("SONG").setIndicator("음악별");
        tabSpecSong.setContent(R.id.tabSong);
        tabHost.addTab(tabSpecSong);

        TabHost.TabSpec tabSpecArtist = tabHost.newTabSpec("ARTIST").setIndicator("가수별");
        tabSpecArtist.setContent(R.id.tabArtist);
        tabHost.addTab(tabSpecArtist);

        TabHost.TabSpec tabSpecAlbum = tabHost.newTabSpec("ALBUM").setIndicator("앨범");
        tabSpecAlbum.setContent(R.id.tabAlbum);
        tabHost.addTab(tabSpecAlbum);

        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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs">
        </TabWidget>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent">
            <LinearLayout
                android:orientation="vertical"
                android:id="@+id/tabSong"
                android:background="#f00000"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:id="@+id/tabArtist"
                android:background="#f0f000"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </LinearLayout>
            <LinearLayout
                android:orientation="vertical"
                android:id="@+id/tabAlbum"
                android:background="#f000ff"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

 

java에서 MainActivity가 상속 받는 것이 TabActivity라는 것을 조심하자.

또 xml에서 id를 지정할 때 TabHost와 TabWidget, FrameLayout은 id가 지정되어 있으므로 조심하자.(임의로 지정 할 수 없다.)

 

실행화면

 

 

'안드로이드 > 연습' 카테고리의 다른 글

[Android] ContextMenu  (0) 2021.08.23
[Android] Menu xml  (0) 2021.08.23
[Android] ViewFlipper  (0) 2021.08.22
[Android] 스크롤뷰  (0) 2021.08.18
[Android] 슬라이딩드로어  (0) 2021.08.18