2swan
Android 탭 호스트 예제 본문
tab1.xml
<?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"
tools:context=".MainActivity7_tab"
android:id="@android:id/tabhost">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tabSong"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#8FA3B3">
</LinearLayout>
<LinearLayout
android:id="@+id/tabArtist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#BA7B7B">
</LinearLayout>
<LinearLayout
android:id="@+id/tabAlbum"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#A5872C">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
tab1.java
public class MainActivity7_tab extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity7_tab);
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);
}
}
결과 값
tab2.xml
<?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"
tools:context=".MainActivity_tab2"
android:id="@android:id/tabhost">
<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">
<ImageView
android:id="@+id/DOG"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/dog"/>
<ImageView
android:id="@+id/CAT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/cat"/>
<ImageView
android:id="@+id/RAB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@drawable/rabbit"/>
<ImageView
android:id="@+id/HORSE"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="#00BCD4">
</TabWidget>
</LinearLayout>
</TabHost>
tab2.java
public class MainActivity_tab2 extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_tab2);
TabHost tabHost = getTabHost();
TabHost.TabSpec tabSpecDog= tabHost.newTabSpec("Dog").setIndicator("강아지");
tabSpecDog.setContent(R.id.DOG);
tabHost.addTab(tabSpecDog);
TabHost.TabSpec tabSpecCAT= tabHost.newTabSpec("CAT").setIndicator("고양이");
tabSpecCAT.setContent(R.id.CAT);
tabHost.addTab(tabSpecCAT);
TabHost.TabSpec tabSpecRAB= tabHost.newTabSpec("RAB").setIndicator("토끼");
tabSpecRAB.setContent(R.id.RAB);
tabHost.addTab(tabSpecRAB);
TabHost.TabSpec tabSpecHORSE= tabHost.newTabSpec("HORSE").setIndicator("말");
tabSpecHORSE.setContent(R.id.HORSE);
tabHost.addTab(tabSpecHORSE);
}
}
결과 값
'Programming > Android Example' 카테고리의 다른 글
Android 에러 : Error running 'app':The activity must be exported or contain an intent-filter (0) | 2023.08.03 |
---|---|
Android 간단한 웹브라우저 앱 (0) | 2023.08.03 |
Android 메뉴 (목록 대화상자) (0) | 2023.08.02 |
Android 컨텍스트 메뉴 (배경색 변경) (0) | 2023.08.02 |
Android 메뉴 (배경색 변경) (0) | 2023.08.02 |