android学习笔记(五) Tab使用

来源:互联网 发布:淘宝分享链接生成器 编辑:程序博客网 时间:2024/05/22 02:28

tab,我把它理解为按钮的一种

基本就类似于选项卡什么的,需要将这种控件至于名为TabHost的容器中使用,也就是说TabHost是tab的载体,因此TabHost有如下方法

public void addTab (TabHost.TabSpec tabSpec) 添加tab,参数TabHost.TabSpec通过下面的函数返回得到
public TabHost.TabSpec newTabSpec (String tag) 创建TabHost.TabSpec
public void clearAllTabs () remove所有的Tabs
public int getCurrentTab ()
public String getCurrentTabTag ()
public View getCurrentTabView ()
public View getCurrentView ()
public FrameLayout getTabContentView () 返回Tab content的FrameLayout
public TabWidget getTabWidget ()
public void setCurrentTab (int index)       设置当前显示的Tab by index
public void setCurrentTabByTag (String tag) 设置当前显示的Tab by tag
public void setOnTabChangedListener (TabHost.OnTabChangeListener l) 设置TabChanged事件的响应处理

那么tab这个控件的定义类的名字为TabSpec,主要有三个属性:
indicator, content, 和 tag,其中indicator为tab按钮上面的文字,cotent则是tab要显示的内容。可使用方法如下:
public String getTag ()
public TabHost.TabSpec setContent
public TabHost.TabSpec setIndicator
有一个现成的acvitity可供我们方便的开发此类程序TabActivity,我们通过getTabHost () 获得当前TabActivity的TabHost,
下面是我的测试代码
private TabHost mytabhost;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //setContentView(R.layout.main);                mytabhost = getTabHost();             LayoutInflater.from(this).inflate(R.layout.main, mytabhost.getTabContentView(), true);                 mytabhost.addTab(mytabhost.newTabSpec("tab1").setIndicator("t1").setContent(R.id.view1));        mytabhost.addTab(mytabhost.newTabSpec("tab2").setIndicator("t2").setContent(R.id.view2));        mytabhost.addTab(mytabhost.newTabSpec("tab3").setIndicator("t3").setContent(R.id.view3));            }

关于TabSpec的content,有以下几种选择:view的id,或使用TabHost.TabContentFactory创建一个view,或者是一个可以启动activity的intent

原创粉丝点击