Android中使用TabHost实现类似标签栏的效果


MainActivity.java

public class MainActivity extends TabActivity {
	 private TabHost tabHost;  
	

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        
      //加载底部Tab布局   
        LinearLayout tab1=(LinearLayout)LayoutInflater.from(this).inflate(R.layout.item, null);  
        final ImageView icon1=(ImageView)tab1.findViewById(R.id.icon);         
        icon1.setBackgroundResource(R.drawable.icon11);  
        final TextView title1=(TextView)tab1.findViewById(R.id.title);  
        title1.setText("First"); 
          
        LinearLayout tab2=(LinearLayout)LayoutInflater.from(this).inflate(R.layout.item, null);  
        ImageView icon2=(ImageView)tab2.findViewById(R.id.icon);  
        icon2.setBackgroundResource(R.drawable.icon12);  
        TextView title2=(TextView)tab2.findViewById(R.id.title);  
        title2.setText("Second");     
          
        // 加载TabSpec   
       /* tabHost = (TabHost) findViewById(R.id.view_tab_host);  
        tabHost.setup(getLocalActivityManager());  */
        tabHost = getTabHost(); 
         
       
        
        //tabHost.setup();
  
        TabSpec ts1 = tabHost.newTabSpec("Activity1");  
        ts1.setIndicator(tab1);
        
        ts1.setContent(new Intent(this, FirstActivity1.class));  
        tabHost.addTab(ts1);  
      
        TabSpec ts2 = tabHost.newTabSpec("Activity2");  
        ts2.setIndicator(tab2);  
           
        ts2.setContent(new Intent(this, SecondActivity2.class));  
        tabHost.addTab(ts2);  
  
        //设置标签栏背景图片   
        TabWidget tw = tabHost.getTabWidget();  
        tw.setBackgroundResource(R.drawable.tabbarbg);  
       
   
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    
    
}




main.xml

  
  
  
      
  
          
  
              
              
  
              
              
          
      
  
 


 
   
  

item.xml

  
  

      
      
 

运行结果:

Android中使用TabHost实现类似标签栏的效果_第1张图片Android中使用TabHost实现类似标签栏的效果_第2张图片










你可能感兴趣的:(Android)