안녕하세요!

 

안드로이드 개발중에 어려움을 느껴 도저히 방법을 못찾아 이렇게 질문을 올려봅니다.

 

Custom Tabbar 를 (이미지와 텍스트)구현하고 넘어 가려 하는데 문제에 들어섰습니다.

 

개발용 디바이스(갤럭시3)에 탭바를 올리고 확인 후 넥서스5에 올려서 확인을 했는데 사이즈에 문제가 생겼습니다.

 

해상도 문제인지 알지만 질문의 요지는 사이즈를 바꿔도 바뀌지 않는다는 점입니다.

 

SetupTab 함수에서 spec.setContent(new Intent(this, Live_Activity_1.class)); 해주는 부분에서 문제가 생기는 것 같습니다.

 

근데 이해가 안되는건 저는 사이즈를 정의해주지 않았는데 무조껀 120의 크기로 맞춰진다는 점입니다.

 

이미지를 빼고 사이즈를 바꾸고 해봤지만 요지부동입니다.. 전혀 안바뀝니다.

 

 

  for(int i=0; i<4; i++)
  {
   cGlobal.mtabHost.getTabWidget().getChildAt(i).setLayoutParams(new LinearLayout.LayoutParams(width,height));
   
  }

 

이 코드를 이용하여 사이즈를 바꾸려 시도해봤지만, System.out.println 을 찍어서 사이즈를 확인해보면

 

사이즈는 바뀐 것으로 나오는데 실제 크기는 전혀 변하지 않습니다.. 120px 에서 절대 움직이지 않습니다.

 

주석처리 해놓앗지만

 

spec.setContent(new TabHost.TabContentFactory() {  
   @Override
   public View createTabContent(String tag) {
    
    return findViewById(android.R.id.tabcontent);
   }
  });

 

createTabContent로 구성하면 사이즈가 자유롭게 변하는걸로 보아 setContent의 문제인 것으로 보이는데 도저히 감이 잡히지 않습니다.

 

(createTabContent로 구성할 수가 없습니다.. intent를 사용해야 하기 때문에..)

 

혹시 해결 방법이나 이유에 대해 아시는분은 답변 부탁드립니다..ㅠㅠ

 

긴글 읽어주셔서 감사합니다.

 

 

 

커스텀 탭바 만든 구성에 대해 먼저 말씀드리겠습니다.

 

tab_layout.xml 입니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:baselineAligned="false"
                android:gravity="fill_vertical|fill_horizontal"
                android:measureWithLargestChild="false" />

                <FrameLayout
                    android:id="@+android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    />
        </LinearLayout>
    </TabHost>
</LinearLayout>

 

탭 호스트를 정의해 주었습니다.

 

items_in_tabwidget.xml 입니다.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabs_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top|center"
    android:orientation="vertical" >
   
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        >

            <ImageView
          android:id="@+id/tabs_image"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="5dp"
          />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:layout_weight="00">
   <TextView
          android:id="@+id/tabs_text"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"      
          android:textSize="12dp"
          android:textColor="@android:color/white"
          />
    </LinearLayout>
</LinearLayout>

탭 widget 각각에 들어갈 내부를 구성해주었습니다.

 

 

 

Main_Tap_Activity 입니다.

 

public class Main_Tap_Activity extends TabActivity{
 
 int menu_off[] = {
   R.drawable.tab_live_img,
   R.drawable.tab_record_img,
   R.drawable.tab_alarm_img,
   R.drawable.tab_setting_img};
 
 int menu_on[] = {
   R.drawable.tab_live_img_on,
   R.drawable.tab_record_img_on,
   R.drawable.tab_alarm_img_on,
   R.drawable.tab_setting_img_on
 };
 
 //String[] tag = {"실시간", "녹화", "알람", "환경설정"};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.tab_layout);
  
  System.out.println("getTabHost");
  cGlobal.mtabHost = getTabHost();
  cGlobal.mtabHost.setup();
  //cGlobal.mtabHost.getTabWidget().setDividerDrawable(R.drawable.mainlogo);
  
  SetupTab(new TextView(this), "실시간");
  SetupTab(new TextView(this), "녹화");
  SetupTab(new TextView(this), "알람");
  SetupTab(new TextView(this), "환경설정");
  
  
  Display display = getWindowManager().getDefaultDisplay();
  int width = display.getWidth();
  width = width/4;
  int height = display.getHeight();
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
  
  for(int i=0; i<4; i++)
  {
   cGlobal.mtabHost.getTabWidget().getChildAt(i).setLayoutParams(new LinearLayout.LayoutParams(width,height));
   
  }
  
  
  cGlobal.mtabHost.getTabWidget().setCurrentTab(0);
  
  
  
  cGlobal.mtabHost.setOnTabChangedListener(new OnTabChangeListener() {
   
   @Override
   public void onTabChanged(String tabId) {
    
    // TODO Auto-generated method stub
    //선택되지 않은 이미지
    for(int i=0; i<cGlobal.mtabHost.getTabWidget().getChildCount(); i++)
    {
     TextView tv = (TextView)cGlobal.mtabHost.getTabWidget().getChildAt(i).findViewById(R.id.tabs_text);
     tv.setTextColor(Color.parseColor("#ffffff"));

     ImageView iv = (ImageView)cGlobal.mtabHost.getTabWidget().getChildAt(i).findViewById(R.id.tabs_image);
     iv.setImageDrawable(getResources().getDrawable(menu_off[i]));
     
     LinearLayout lv = (LinearLayout)cGlobal.mtabHost.getTabWidget().getChildAt(i).findViewById(R.id.tabs_layout);
     lv.setBackgroundColor(Color.parseColor("#212121"));
    }
    
    //선택 된 이미지
    TextView tv = (TextView)cGlobal.mtabHost.getTabWidget().getChildAt(cGlobal.mtabHost.getCurrentTab()).findViewById(R.id.tabs_text);
    tv.setTextColor(Color.parseColor("#ffffff"));

    ImageView iv = (ImageView)cGlobal.mtabHost.getTabWidget().getChildAt(cGlobal.mtabHost.getCurrentTab()).findViewById(R.id.tabs_image);
    iv.setImageDrawable(getResources().getDrawable(menu_on[cGlobal.mtabHost.getCurrentTab()]));

    LinearLayout lv = (LinearLayout)cGlobal.mtabHost.getTabWidget().getChildAt(cGlobal.mtabHost.getCurrentTab()).findViewById(R.id.tabs_layout);
     lv.setBackgroundColor(Color.parseColor("#636363"));
   }
  });
  
 }
 
 private void SetupTab(final View view, final String tag)
 {
        /*
        TabHost.TabSpec spec = cGlobal.mtabHost.newTabSpec(tag[0]);
        spec.setContent(new Intent(this, Live_Activity_1.class));
        spec.setContent(new TabHost.TabContentFactory() {  
   @Override
   public View createTabContent(String tag) {
    
    return findViewById(android.R.id.tabcontent);
   }
  });
        spec.setIndicator(createTabView(cGlobal.mtabHost.getContext(), tag[0]));
        cGlobal.mtabHost.addTab(spec);
       
       
        spec = cGlobal.mtabHost.newTabSpec(tag[1]);
        spec.setContent(new TabHost.TabContentFactory() {
   
   @Override
   public View createTabContent(String tag) {
    
    return findViewById(android.R.id.tabcontent);
   }
  });
       
        spec.setIndicator(createTabView(cGlobal.mtabHost.getContext(), tag[1]));
        cGlobal.mtabHost.addTab(spec);
       
       
        spec = cGlobal.mtabHost.newTabSpec(tag[2]);
        spec.setContent(new TabHost.TabContentFactory() {
   
   @Override
   public View createTabContent(String tag) {
    
    return findViewById(R.id.tabcontent);
   }
  });
       
        spec.setIndicator(createTabView(cGlobal.mtabHost.getContext(), tag[2]));
        cGlobal.mtabHost.addTab(spec);
       
        spec = cGlobal.mtabHost.newTabSpec(tag[3]);
        spec.setContent(new TabHost.TabContentFactory() {
   
   @Override
   public View createTabContent(String tag) {
    
    return findViewById(R.id.tabcontent);
   }
  });
       
        spec.setIndicator(createTabView(cGlobal.mtabHost.getContext(), tag[3]));
        cGlobal.mtabHost.addTab(spec);
        */
       
       
        // TabSpec은 공개된 생성자가 없으므로 직접 생성할 수 없으며, TabHost의 newTabSpec메서드로 생성

  View tabview = createTabView(cGlobal.mtabHost.getContext(), tag);
        TabSpec spec = cGlobal.mtabHost.newTabSpec(tag).setIndicator(tabview);
    
        if(tag.equals("실시간"))
         spec.setContent(new Intent(this, Live_Activity_1.class));
        else if(tag.equals("녹화"))
         spec.setContent(new Intent(this, Search_Display.class));
        else if(tag.equals("알람"))
         spec.setContent(new Intent(this, MainActivity.class));
        else if(tag.equals("환경설정"))
         spec.setContent(new Intent(this, Setting.class));
       
            cGlobal.mtabHost.addTab(spec);
 }
 
 // Tab에 나타날 View를 구성
    private static View createTabView(final Context context, final String text)
    {
        // layoutinflater를 이용해 xml 리소스를 읽어옴
        View view = LayoutInflater.from(context).inflate(R.layout.items_in_tabwidget, null);
        ImageView img;
        LinearLayout lv;
        
        if(text.equals("실시간"))
        {
         img = (ImageView)view.findViewById(R.id.tabs_image);
         img.setImageResource(R.drawable.tab_live_img_on);
         
            lv = (LinearLayout)view.findViewById(R.id.tabs_layout);
            lv.setBackgroundColor(Color.parseColor("#636363"));
        }
        else if(text.equals("녹화"))
        {
         img = (ImageView)view.findViewById(R.id.tabs_image);
         img.setImageResource(R.drawable.tab_record_img);
         
            lv = (LinearLayout)view.findViewById(R.id.tabs_layout);
            lv.setBackgroundColor(Color.parseColor("#212121"));
        }
        else if(text.equals("알람"))
        {
         img = (ImageView)view.findViewById(R.id.tabs_image);
         img.setImageResource(R.drawable.tab_alarm_img);
         
            lv = (LinearLayout)view.findViewById(R.id.tabs_layout);
            lv.setBackgroundColor(Color.parseColor("#212121"));
        }
        else if(text.equals("환경설정"))
        {
         img = (ImageView)view.findViewById(R.id.tabs_image);
         img.setImageResource(R.drawable.tab_setting_img);
         
            lv = (LinearLayout)view.findViewById(R.id.tabs_layout);
            lv.setBackgroundColor(Color.parseColor("#212121"));
        }
       
        TextView tv = (TextView) view.findViewById(R.id.tabs_text);
        tv.setText(text);      
       
       
        return view;
    }
}