IT/Android

Container - TabHost

바바옄 2015. 6. 18. 12:40
반응형

TabHost 1

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.ktds.myframelayout;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
 
public class MainActivity extends ActionBarActivity {
 
    private TextView tvOne;
    private TextView tvTwo;
    private Button btnChangeLayer;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tvOne = (TextView)findViewById(R.id.tvOne);
        tvTwo = (TextView)findViewById(R.id.tvTwo);
 
        tvTwo.setVisibility(View.GONE);
 
        btnChangeLayer =(Button)findViewById(R.id.btnChangeLayer);
 
        btnChangeLayer.setOnClickListener(new View.OnClickListener(){
 
            @Override
            public void onClick(View v) {
                if(tvOne.getVisibility() == View.GONE) {
                    tvOne.setVisibility(View.VISIBLE);
                    tvTwo.setVisibility(View.GONE);
                }
                else{
                    tvOne.setVisibility(View.GONE);
                    tvTwo.setVisibility(View.VISIBLE);
                }
            }
        });
    }
 
}
 
cs

 

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <Button
        android:id="@+id/btnChangeLayer"
        android:text="CHANGE"
        android:textSize="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <TextView
            android:id="@+id/tvOne"
            android:text="One"
            android:textSize="30dp"
            android:textColor="#333333"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tvTwo"
            android:text="Two"
            android:textSize="30dp"
            android:textColor="#789342"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
 
    </FrameLayout>
 
</LinearLayout>
 
cs

 

          

 

TabHost 2

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.ktds.mytabhost;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TabHost;
 
 
public class MainActivity extends ActionBarActivity {
 
    private TabHost tabHost;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tabHost = (TabHost) findViewById(R.id.tabHost);
        //TabHost를 설정할 때 가장 먼저 호출해 주어야함
        tabHost.setup();
 
        //텝버튼을 만들어줌
        TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1");
        tab1.setContent(R.id.tab1);
        tab1.setIndicator("-Tab1-");
 
        TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
        tab2.setContent(R.id.tab2);
        tab2.setIndicator("-Tab2-");
 
        TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3");
        tab3.setContent(R.id.tab3);
        tab3.setIndicator("-Tab3-");
 
        //TabHost에 TabSpec를 등록함
        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);
    }
 
}
 
cs

 

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabHost">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
 
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></TabWidget>
 
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
 
                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
 
                    <!--
                        tab1에서 보여줄거 적기
                    -->
                    <TextView
                        android:text="첫번째 tab"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
 
                </LinearLayout>
 
                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
 
                    <!--
                        tab2에서 보여줄거 적기
                    -->
                    <TextView
                        android:text="두번째 tab"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
 
 
                </LinearLayout>
 
                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
 
                    <!--
                        tab3에서 보여줄거 적기
                    -->
                    <TextView
                        android:text="세번째 tab"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
 
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
 
</LinearLayout>
 
cs

 

         

 

 

 

반응형

'IT > Android' 카테고리의 다른 글

Layout  (0) 2015.06.22
ListView  (0) 2015.06.18
Widget - WebView  (0) 2015.06.17
Widget - Layout  (0) 2015.06.17
Widget - ProgressBar  (0) 2015.06.17