TabHost两种实现方式

阅读更多
第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。只要定义具体Tab内容布局就行了.

xml布局:

	
		
		
		
	
	
		
		
	
	
		
			
			
			
			
		
	


java代码:
super.onCreate(savedInstanceState);
		myTabhost=this.getTabHost();
		//get Tabhost
		LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(), true);
		myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));
		
		myTabhost
				.addTab(myTabhost.newTabSpec("One")// make a new Tab
						.setIndicator("A")
						// set the Title and Icon
						.setContent(R.id.widget_layout_Blue));
		// set the layout

		myTabhost
				.addTab(myTabhost.newTabSpec("Two")// make a new Tab
						.setIndicator("B",
								getResources().getDrawable(R.drawable.mumule))
						// set the Title and Icon
						.setContent(R.id.widget_layout_green));
		// set the layout

		myTabhost
				.addTab(myTabhost.newTabSpec("Three")// make a new Tab
						.setIndicator("C",
								getResources().getDrawable(R.drawable.notepad))
						// set the Title and Icon
						.setContent(R.id.widget_layout_red));


第二种:不用继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是
@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。TabHost的id可以自定义.

xml布局:

 
    
     	
			
	     	
	        
	     
		     
			      	
			    	
			    	
		     
	     
	     
    


java代码:
protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hometabs);
        
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        TabWidget tabWidget = tabHost.getTabWidget();
        
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("tab1", getResources().getDrawable(R.drawable.mumule))
                .setContent(R.id.view1));
        
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("tab3")
                .setContent(R.id.view3));
        
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("tab2")
                .setContent(R.id.view2));
        
        final int tabs = tabWidget.getChildCount();
        Log.i(TAG, "***tabWidget.getChildCount() : " + tabs);
        
        final int tabWidth = 90;
		final int tabHeight = 45;
		
		for (int i = 0; i < tabs; i++) {
		/*	final View view = tabWidget.getChildAt(i);
			view.getLayoutParams().width = tabWidth;
			view.getLayoutParams().height = tabHeight;
			final TextView tv = (TextView) view.findViewById(android.R.id.title);
		    tv.setTextColor(this.getResources().getColorStateList(android.R.color.black));
		    MarginLayoutParams tvMLP = (MarginLayoutParams)tv.getLayoutParams();
		    tvMLP.bottomMargin = 8;*/
		}
	}
  • Tab.rar (130.9 KB)
  • 下载次数: 1250

你可能感兴趣的:(Android,XML,C,C++,C#)