android学习日记03--常用控件tabSpec/tabHost


7、TabSpec和TabHost
TabSpec相当于浏览器的分页,而TabHost就相当于分页的集合
TabSpec分页可以相当于一个独立的组件,也可以是一个布局,
将每个分页装进TabHost里

Activity extends TabActivity
以前都是 继承android.app.Activity
这里为了获得Activity分页菜单,继承她

创建分页菜单对象,将activity_main装进去
tableHost = this.getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_main,tableHost.getTabContentView());

通过newTabSpec 来创建分页
ts1 = tableHost.newTabSpec("tab1");

设置名称和背景图
ts2.setIndicator("tab2", getResources().getDrawable(R.drawable.bg));

添加组件
ts2.setContent(R.id.et);

通过addtab加进去
tableHost.addTab(ts1);
tableHost.addTab(ts2);
tableHost.addTab(ts3);

设置监听
tableHost.setOnTabChangedListener(this);

arg0 表示传入的tab名称
@Override
public void onTabChanged(String arg0) {
if (arg0.equals("tab1")) {
Toast.makeText(this, "分页1", Toast.LENGTH_LONG).show();
} else if(arg0.equals("tab2")){
Toast.makeText(this, "分页2", Toast.LENGTH_LONG).show();
}else if(arg0.equals("tab3")){
Toast.makeText(this, "分页3", Toast.LENGTH_LONG).show();
}
}

完整代码和运行效果如下:

android学习日记03--常用控件tabSpec/tabHost
 1 package com.example.tabproject;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.app.TabActivity;
 6 import android.view.LayoutInflater;
 7 import android.view.Menu;
 8 import android.widget.TabHost;
 9 import android.widget.Toast;
10 import android.widget.TabHost.OnTabChangeListener;
11 import android.widget.TabHost.TabSpec;
12 
13 public class MainActivity extends TabActivity implements OnTabChangeListener{
14 
15     private TabSpec ts1,ts2,ts3;
16     private TabHost tableHost;
17     
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         
22         tableHost = this.getTabHost();
23         
24         LayoutInflater.from(this).inflate(R.layout.activity_main,tableHost.getTabContentView());
25         
26         ts1 = tableHost.newTabSpec("tab1");
27         ts1.setIndicator("tab1");
28         ts1.setContent(R.id.btn);
29         
30         ts2 = tableHost.newTabSpec("tab2");
31         ts2.setIndicator("tab2", getResources().getDrawable(R.drawable.bg));
32         ts2.setContent(R.id.et);      
33         
34         ts3 = tableHost.newTabSpec("tab3");
35         ts3.setContent(R.id.layout);
36         ts3.setIndicator("tab3", getResources().getDrawable(R.drawable.pg2));
37         ts3.setContent(R.id.et);             
38         
39         tableHost.addTab(ts1);
40         tableHost.addTab(ts2);
41         tableHost.addTab(ts3);
42         
43         tableHost.setOnTabChangedListener(this);
44         
45         
46 
47     }
48 
49 
50     @Override
51     public boolean onCreateOptionsMenu(Menu menu) {
52         // Inflate the menu; this adds items to the action bar if it is present.
53         getMenuInflater().inflate(R.menu.main, menu);
54         return true;
55     }
56 
57 
58     @Override
59     public void onTabChanged(String arg0) {
60         // TODO Auto-generated method stub
61         if (arg0.equals("tab1")) {
62             Toast.makeText(this, "分页1", Toast.LENGTH_LONG).show();
63         } else if(arg0.equals("tab2")){
64             Toast.makeText(this, "分页2", Toast.LENGTH_LONG).show();
65         }else if(arg0.equals("tab3")){
66             Toast.makeText(this, "分页3", Toast.LENGTH_LONG).show();
67         }
68     }
69     
70 }
View Code

android学习日记03--常用控件tabSpec/tabHost

android学习日记03--常用控件tabSpec/tabHost

上一篇:【转】 Android经验: proguard 阻碍 webview 正常工作


下一篇:Android : Your APK does not seem to be designed for tablets.