IT/Android

android - TabHost와 LinearLayout, TabHost와 ImageView

노마드오브 2018. 9. 17. 23:07

파일명 : activity_main.xml

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

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

</LinearLayout>

</TabHost>


파일명 : MainActivity.java

package com.example.it.myapplication6_17;

import android.app.TabActivity;
import android.support.v7.app.AppCompatActivity;
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 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);
}

}





파일명 : activity_main.xml

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

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

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tabSong"
android:background="#f00000"
android:src="@drawable/cat"
>
</ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tabArtist"
android:src="@drawable/dog" >
</ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tabAlbum"
android:src="@drawable/horse" >
</ImageView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tab4"
android:src="@drawable/rabbit" >
</ImageView>

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

</LinearLayout>

</TabHost>


파일명 : MainActivity.java

package com.example.it.myapplication6_18;

import android.app.TabActivity;
import android.support.v7.app.AppCompatActivity;
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 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.TabSpec tabSpecRabbit = tabHost.newTabSpec("RABBIT").setIndicator("토끼");
tabSpecRabbit.setContent(R.id.tab4);
tabHost.addTab(tabSpecRabbit);

tabHost.setCurrentTab(0);
}

}