Android > ActionBar - Tab ¸Þ´º¿¡ CustomView ÀÌ¿ëÇÏ¿© ½ºÅ¸ÀÏ Àû¿ëÇÏ´Â ¹æ¹ý
µî·ÏÀÏ : 2017-07-05 18:58
Á¶È¸¼ö : 52,975
Áö³¹ø¿¡ Action Bar Style º¯°æ ¹× Å׸¶ º¯°æ ¹æ¹ýÀ» ¾Ë¾Æº¸¾Ò´Âµ¥¿ä. ¿À´ÃÀº Tab ¸Þ´º¿¡ ³»°¡ ¿øÇÏ´Â µðÀÚÀÎÀÇ customviewÀ» Àû¿ëÇÏ´Â ¹æ¹ý¿¡ ´ëÇؼ
¾Ë¾Æº¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
±×¸²ÀÇ Tab ¸Þ´º¸¦ º¸¸é ¸ð¾çÀº °¢°¢ ´Ù¸£°ÚÁö¸¸, ¾ÆÀÌÄÜ + ÅؽºÆ® ÇüÅÂÀÇ Tab ¸Þ´º¸¦ ±¸¼ºÇÏ°í ÀÖ½À´Ï´Ù.
±âº»ÀûÀÎ ActionBar Tab ¸Þ´º¿¡¼´Â ³ª¿À±â Èûµç ±¸Á¶ ÀÔ´Ï´Ù.
ÀÌ·±°æ¿ì layout À» ÀâÀº ´ÙÀ½¿¡ Tab ÀÇ View·Î µî·Ï¸¸ ÇÏ¸é µË´Ï´Ù.
layout_actionbar_tab.xml ÆÄÀÏÀ» ¸¸µçÈÄ Imageview ¿Í Textview ¸¦ À§Ä¡½Ãŵ´Ï´Ù.
res/layout/layout_actionbar_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/tabIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.04"
android:paddingTop="2dp" />
<TextView
android:id="@+id/tabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF" />
</LinearLayout>
tabIcon + tabText µÎ°³¸¦ ¸¸µé¾ú½À´Ï´Ù.
ÀÌÁ¦ java ¼Ò½º¿¡¼ Tab ºÎºÐ¸¸ ¼Ò½º¸¦ º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
// tab
View tabView1 = getLayoutInflater().inflate(R.layout.layout_actionbar_tab, null);
View tabView2 = getLayoutInflater().inflate(R.layout.layout_actionbar_tab, null);
View tabView3 = getLayoutInflater().inflate(R.layout.layout_actionbar_tab, null);
TextView tabText1 = (TextView) tabView1.findViewById(R.id.tabText);
TextView tabText2 = (TextView) tabView2.findViewById(R.id.tabText);
TextView tabText3 = (TextView) tabView3.findViewById(R.id.tabText);
tabText1.setText(R.string.sometext1);
tabText2.setText(R.string.sometext2);
tabText3.setText(R.string.sometext3);
ImageView tabImage1 = (ImageView) tabView1.findViewById(R.id.tabIcon);
ImageView tabImage2 = (ImageView) tabView2.findViewById(R.id.tabIcon);
ImageView tabImage3 = (ImageView) tabView3.findViewById(R.id.tabIcon);
tabImage1.setImageDrawable(getResources().getDrawable(R.drawable.btn_notice));
tabImage2.setImageDrawable(getResources().getDrawable(R.drawable.btn_notice));
tabImage3.setImageDrawable(getResources().getDrawable(R.drawable.btn_notice));
Tab tab1 = actionBar.newTab();
//tab1.setText("¸Þ´º1");
//tab1.setIcon(R.drawable.btn_notice);
tab1.setCustomView(tabView1);
TabTestListener a = new TabTestListener(this, "1", Fragment01.class);
tab1.setTabListener(a);
Tab tab2 = actionBar.newTab();
//tab2.setText("¸Þ´º2");
//tab2.setIcon(R.drawable.popup_background);
tab2.setCustomView(tabView2);
TabTestListener b = new TabTestListener(this, "1", Fragment02.class);
tab2.setTabListener(b);
Tab tab3 = actionBar.newTab();
//tab3.setText("tab_3");
//tab3.setIcon(R.drawable.btn_notice);
tab3.setCustomView(tabView3);
TabTestListener c = new TabTestListener(this, "1", Fragment03.class);
tab3.setTabListener(c);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
¼Ò½º¸¦ º¸¸é ¾Æ½Ã°ÚÁö¸¸ tabView1, tabView2, tabview3 ÀÇ View °´Ã¼¿¡ ³Ö°í layout_actionbar_tab.xml ÆÄÀÏÀ» ÀÐ¾î µå¸³´Ï´Ù.
layout_actionbar_tab ¿¡ ÀÖ´Â tabText ¿Í tabIcon °´Ã¼µµ ÀÐ¾î µå¸°´Ù. °¢±â ´Ù¸¥ ¾ÆÀÌÄÜ°ú text ¸¦ ¿¬°á½ÃÅ°¸é µË´Ï´Ù.
Tab ¸Þ´º¿¡ Ç¥½Ã´Â ¹æ¹ýÀº tab1.setCustomView(tabView1) , tab2.setCustomView(tabView2) , tab3.setCustomView(tabView3) À» ÅëÇØ Ç¥½ÃÇÒ ¼ö ÀÖ½À´Ï´Ù.