Mobile/Android

[안드로이드]탭뷰 만들기

saltdoll 2011. 4. 6. 19:16
반응형
출처: Android 입문자를 위한 강좌 - 7. 탭뷰의 이용


프로젝트하면서 이용했던 탭뷴데 , 다시 정리해둬야 할 것 같아서!

탭뷰는 조금 조잡하다 .
XML에서 탭뷰의 구성요소는 크게 3가지.
<TabHost>, <TabWidget> , <Layout>이다.

TabHost는 Tab을 보유한 레이아웃이며 ,
TabWidget은 흔히들 생각하는 탭,
Layout은 탭에따라 전환되는 "뷰"이다.
보통 전환할 수 있도록 FrameLayout안에 Linear등의 일반 레이아웃을 배치하는 것이 일반적이다.

Java에서 탭뷰 연결방법은 다음과 같다.

1. TabHost를 얻어온다 (findViewById)
2. Host의 setup함수 호출 ( 탭들을 add하기 위한 준비작업 )
3. Host의 newTabSpec을 통해 탭메뉴들을 생성.
4. TabSpec.setIndicater로 탭에 표시될 내용 지정
5. setContent로 XML에서 선언한 전환 뷰 지정
6. Host에 add
7. 3~6을 원하는 탭 메뉴 수만큼 추가
8. setCurrentTab으로 처음 탭뷰 진입했을때의 화면 지정.

탭뷰의 높이 또한 지정 가능하다.
모두 추가한뒤(7)에


1
2
3
        for(int tab = 0; tab < tab_host.getTabWidget().getChildCount(); ++tab){
            tab_host.getTabWidget().getChildAt(tab).getLayoutParams().height= 50;
        }

와 같이 for문을 작성하면 해당 height가 변경된다.

다음은 위에서 설명한 과정의 Java 소스 예이다.
1             
2
3
4
5
6
7
8
TabHost tab_host = (TabHost)findViewById(R.id.tab_host);
tab_host.setup();
          
          
 TabSpec ts1 = tab_host.newTabSpec("TAB_HOME");
 ts1.setIndicator("home");
 ts1.setContent(R.id.home);
 tab_host.addTab(ts1);


다음은 XML코드의 일부분이다.

1<TabHost
2    android:layout_width="fill_parent"
3    android:layout_height="fill_parent"
4    android:id="@+id/tab_host"
5>
6<TabWidget
7    android:id="@android:id/tabs"
8    android:layout_width="fill_parent"
9    android:layout_height="wrap_content"
10    android:paddingTop="430px"
11    android:background="#ffffffff"
12/>
13    <FrameLayout
14        android:id = "@android:id/tabcontent"
15        android:layout_width ="fill_parent"
16        android:layout_height="fill_parent"
17        android:paddingBottom="50px"
18       
19    >
20        <LinearLayout
21            android:id= "@+id/home"

여기서 전환뷰의 아이디로 home이라고 지정했기때문에 setcontent에서 home으로 지정이 가능하다.
;; 사진은 우리 캠퍼스의 상징물. 암튼 탭뷰만을 보실때 실행화면은 다음과 같다.

반응형
도움이 되셨다면 하트모양의 "♡ 공감"을 눌러주시면 큰 격려가 됩니다.
(로그인하지 않으셔도 가능)