Android > ActionBar - Tab ±¸Çö ¹æ¹ý
µî·ÏÀÏ : 2017-07-05 18:52
Á¶È¸¼ö : 53,133
¾×¼Ç¹Ù(ActionBar)¿¡ ÅÇ(Tab)¸¦ ºÙ¿©¼ »ç¿ëÇÏ´Â ¾ÛµéÀÌ ¸¹´õ±º¿ä.
±×·¡¼ À̹ø Æ÷½ºÆ®´Â ActionBar¿¡ Tab¸¦ ±¸ÇöÇÏ´Â ¿¹Á¦¼Ò½ºÀÔ´Ï´Ù.
ActionBar¿¡ TabÀ» ±¸ÇöÇÒ¶§´Â °¡±ÞÀû Fragment¸¦ »ç¿ëÇϽô °ÍÀÌ ÄÚµå°ü¸® ¹× ¸®¼Ò½º°ü¸®¿¡ ¿ëÀÌÇÕ´Ï´Ù.
±×·±µ¥ Fragment±îÁö ÇѲ¨¹ø¿¡ ¼Ò°³ÇÏ¸é ´Ù¼Ò ºÐ·®ÀÌ ¸¹¾Æ¼ ´Ü°èÀûÀ¸·Î °£´ÜÇÏ°Ô ActionBar¿¡ TabÀ» ±¸ÇöÇÏ°í Çϳª¾¿ ±â´ÉÀ» Ãß°¡ÇÏ¸é¼ ¿¹Á¦¸¦ ¼Ò°³ÇÏ°Ú½À´Ï´Ù.
°¢ TabÀÇ ¼±Åÿ¡ µû¶ó ´Ù¸¥ View°¡ º¸À̵µ·Ï Çß½À´Ï´Ù.
ÀÌ ¿¡Á¦´Â ActionBar¿¡ TabÀ» ±¸ÇöÇϴµ¥ ÃÊÁ¡ÀÌ ÀÖ¾î¼ Tab¸¶´Ù º¸¿©Áö´Â View´Â MainActivityÀÇ setContentView()¸Þ¼Òµå¸¦ »ç¿ëÇß½À´Ï´Ù.
Activity Ŭ·¡½ºÀÇ setContentView()´Â ¾ðÁ¦³ª onCreate()¿¡¼¸¸ »ç¿ëÇÑ´Ù°í ¾Æ½Ã´Â ºÐµµ ÀÖ´øµ¥.
Ŭ·¡½º ¾È¿¡ ¾î¶² ¼ø°£¿¡µµ ¸Þ¼Òµå È£ÃâÀÌ °¡´ÉÇÕ´Ï´Ù. Ç×»ó setContentView(R.layout.main) À¸·Î¸¸ »ç¿ëÇÒ ¼ö ÀÖ´Â °Ç ¾Æ´Õ´Ï´Ù.
°¢ Tab¸¶´Ù º¸¿©Áö´Â ViewÀÇ ¸ð¾çÀ»º°µµÀÇ layout ÆÄÀÏ·Î ¸¸µé°Å¶ó¼ layout_main.xmlÀº ÀÌ ¿¹Á¦¿¡¼ º°·Î Áß¿äÇÏÁö ¾Ê½À´Ï´Ù.
±×·³ °¢ TabÀ» ¼±ÅÃÇßÀ» ¶§ º¸¿©Áö´Â ViewÀÇ ¸ð¾çÀ» ¼³°èÇÏ´Â ·¹À̾ƿô ¸®¼Ò½º ÆÄÀÏÀ» ¸¸µé°Ú½À´Ï´Ù.
resÆú´õ >> layout Æú´õ¾È¿¡ 3°³ÀÇ ¼·Î ´Ù¸¥ ·¹À̾ƿô xml ÆÄÀÏÀ» ¸¸µé°Ú½À´Ï´Ù.
resÆú´õ ÇÏÀ§ layout Æú´õ [activity_tab1.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
resÆú´õ ÇÏÀ§ layout Æú´õ [activity_tab2.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<DigitalClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
resÆú´õ ÇÏÀ§ layout Æú´õ [activity_tab3.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp" >
<CalendarView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"/>
</RelativeLayout>
ÀÌÁ¦ °¡Àå Áß¿äÇÑ MainActivity.java ¼Ò½ºÆÄÀÏÀÔ´Ï´Ù.
ÁÖ¿ä ¼³¸íÀº ÁÖ¼®À» Âü°íÇϽñ⠹ٶø´Ï´Ù.
ÁÖ¼®À» »©¸é ÄÚµå´Â ¸Å¿ì °£´ÜÇÕ´Ï´Ù.
¹Ýº¹¹®À» »ç¿ëÇÏ¸é º¸´Ù Äڵ带 °£·«ÇÏ°Ô ¸¸µé ¼ö ÀÖÁö¸¸
º¸½Ã±â ÆíÇϵµ·Ï ÀÏÀÏÈ÷ Tab 3°³¸¦ Ãß°¡Çß½À´Ï´Ù.
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
ActionBar actionBar; //ActionBar ÂüÁ¶º¯¼ö
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ActionBar °´Ã¼¸¦ ¾ò¾î¿À±â.(API 11¹öÀü À̻󿡼 »ç¿ë°¡´É)
actionBar= getActionBar();
//ActionBar°¡ Tab¸¦ º¸¿©ÁÙ ¼ö ÀÖ´Â ¸ð¾çÀÌ µÇµµ·Ï ¼³Á¤
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//ActionBar¿¡ Ãß°¡ µÉ Tab ÂüÁ¶º¯¼ö
Tab tab=null;
//ù¹ø° Tab °´Ã¼ »ý¼º ¹× ActionBar¿¡ Ãß°¡Çϱâ
tab= actionBar.newTab();
tab.setText("Analog"); //Tab¿¡ º¸¿©Áö´Â ±Û¾¾
tab.setTabListener(listener); //TabListener ¼³Á¤
actionBar.addTab(tab);
tab= actionBar.newTab();
tab.setText("Digital");
tab.setTabListener(listener);
actionBar.addTab(tab);
tab= actionBar.newTab();
tab.setText("Calendar");
tab.setTabListener(listener);
actionBar.addTab(tab); //ActionBar¿¡ Tab Ãß°¡
}
//Listener °´Ã¼ »ý¼º (TabÀÇ ¼±Åú¯È¸¦ ÀÎÁö - ButtonÀÇ onClickListner ó·³ »ý°¢ÇÏ½Ã¸é µË´Ï´Ù.)
TabListener listener= new TabListener() {
//TabÀÇ ¼±ÅÃÀÌ ¹þ¾î³¯ ¶§ È£Ãâ
//ù¹ø° ÆĶó¹ÌÅÍ : ¼±Åÿ¡¼ ¹þ¾î³ª´Â Tab °´Ã¼
//µÎ¹ø° ÆĶó¹ÌÅÍ : Tab¿¡ ÇØ´çÇÏ´Â View¸¦ Fragment·Î ¸¸µé¶§ »ç¿ëÇÏ´Â Æ®·£Á§¼Ç.(¿©±â¼´Â »ç¿ëX)
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
//TabÀÌ ¼±ÅÃµÉ ¶§ È£Ãâ
//ù¹ø° ÆĶó¹ÌÅÍ : ¼±ÅÃµÈ Tab °´Ã¼
//µÎ¹ø° ÆĶó¹ÌÅÍ : Tab¿¡ ÇØ´çÇÏ´Â View¸¦ Fragment·Î ¸¸µé¶§ »ç¿ëÇÏ´Â Æ®·£Á§¼Ç.(¿©±â¼´Â »ç¿ëX)
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
//¼±ÅÃµÈ Tab°´Ã¼ÀÇ À§Ä¡°ª(¿ÞÁ· óÀ½ºÎÅÍ 0,1,2....¼øÀ¸·Î µÊ)
int position = tab.getPosition();
switch( position ){
case 0:
//°¡Àå ¿ÞÂÊ Tab ¼±ÅÃ(Analog)
//MainActivity°¡ º¸¿© ÁÙ View¸¦
setContentView(R.layout.activity_tab1);
break;
case 1:
//µÎ¹ø° Tab ¼±ÅÃ(Digital)
//MainActivity°¡ º¸¿© ÁÙ View¸¦
setContentView(R.layout.activity_tab2);
break;
case 2:
//¼¼¹ø° Tab ¼±ÅÃ(Calendar)
//MainActivity°¡ º¸¿© ÁÙ View¸¦
setContentView(R.layout.activity_tab3);
break;
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.item1) {
return true;
}
return super.onOptionsItemSelected(item);
}
}