`
gryphone
  • 浏览: 426811 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

TabActivity 使用

阅读更多

TabActivity

 

[功能]

一个TabActivity 包含 数个标签

 

 

[代码 步骤]

1. 得到 TabHost 的实例

TabHost host = this.getTabHost();

 

 

 

2. 定义 CustomTabs 用于填充 TabHost 各个标签布局用

写道
其必须包含 public View createTabContent(String tag) 定义

 

public class CustomTabs implements TabHost.TabContentFactory  {
	Activity activity;
	LayoutInflater inflater;
	
	LinearLayout layout;
	
	public CustomTabs (Activity a) {
		activity = a;
		inflater = activity.getLayoutInflater();
	}
	
	/** {@inheritDoc} */
    public View createTabContent(String tag) {
    		activity.setTitle(tag);
    		
    		return addCustomView(tag);
    }
    
    
    public View addCustomView(String id){
    	
    	layout = new LinearLayout(activity);
        layout.setOrientation(LinearLayout.VERTICAL);
        
        
        if(id.equals(TAB1)){
            ImageView iv = new ImageView(activity);
            iv.setImageResource(R.drawable.o);
            layout.addView(iv,
            		new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
        }
        else if(id.equals(TAB2)){
        	
            EditText edit = new EditText(activity);
            layout.addView(edit,
            		new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            
            Button btn = new Button(activity);
            btn.setText("OK");
            layout.addView(btn,
            		new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            
            RadioGroup group = new RadioGroup(activity);
            group.setOrientation(LinearLayout.HORIZONTAL);
            RadioButton radio1 = new RadioButton(activity);
            radio1.setText("Radio 1");
            group.addView(radio1);
            RadioButton radio2 = new RadioButton(activity);
            radio2.setText("Radio 2");
            group.addView(radio2);
            
            layout.addView(group,
            		new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }
        else if(id.equals(TAB3)){
        	
        	LinearLayout.LayoutParams param3 =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        	
            layout.addView(inflater.inflate(R.layout.item_3, null),param3);
        }
        else if(id.equals(TAB4)){
        	
        	LinearLayout.LayoutParams param4 =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        	
            layout.addView(inflater.inflate(R.layout.item_4, null),param4);
        }
        else {
        	TextView tv5 = new TextView(activity);
        	tv5.setText("no resource aviable!");
        	tv5.setGravity(1);
        	
        	layout.addView(tv5,
            		new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
        }

        return layout;
    }
	
}

 

3. 使用 CustomTabs

host.addTab(host.newTabSpec(TAB1)
                .setIndicator("Tab 1", getResources().getDrawable(R.drawable.beijing_001_mb5ucom))
                .setContent(item));

 

 

4. 标签变化的监听函数

host.setOnTabChangedListener(new OnTabChangeListener(){
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				
			}
        });

 

 

 

5. emulator 运行截图:

 * 标签1

 

* 标签2

 

 

* 标签3

分享到:
评论
2 楼 vs_diy 2010-11-07  
谢谢。我看看
1 楼 xianming90 2010-11-06  
谢谢,最近在学android,这个实例不错

相关推荐

Global site tag (gtag.js) - Google Analytics