Tab Layout的使用

 

 

Tab可以用于选择同一个activity的不同视图,可以以用于选择两个完全不同的activity

创建一个Tab Layout ,必须有一个TabHost 和一个TabWidge, TabHost必须为根节点,它拥有一个TabWidge来呈现tabs和一个FrameLayout来呈现内容。

需要用一个LinearLayout来安置TabWidget和FrameLayou

 

以下是实现Tab的XML 配置

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/tabhost" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout android:orientation="vertical"
		android:layout_width="fill_parent" android:layout_height="fill_parent"
		android:padding="5dp">
		<TabWidget android:id="@android:id/tabs"
			android:layout_width="fill_parent" android:layout_height="wrap_content" />
		<FrameLayout android:id="@android:id/tabcontent"
			android:layout_width="fill_parent" android:layout_height="fill_parent"
			android:padding="5dp" >
		</FrameLayout>
	</LinearLayout>
</TabHost>

 

public class HelloTabWidget extends TabActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab);
		
		Resources res = getResources(); // Resource object to get Drawables   
		TabHost tabHost = getTabHost();  // The activity TabHost    获取一个TabHost 对象 
		TabHost.TabSpec spec;  // Resusable TabSpec for each tab   Tab指示器
		Intent intent;  // Reusable Intent for each tab    
		// Create an Intent to launch an Activity for the tab (to be reused)  

		intent = new Intent().setClass(this, ArtistsActivity.class);    
		// Initialize a TabSpec for each tab and add it to the TabHost
		
		spec = tabHost.newTabSpec("artists"); //实例化一个分页
		spec.setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists));//设置标题内容和图标             
		spec.setContent(intent);  //设置Tab内容 
		tabHost.addTab(spec);    // 添加一个分页对象
			//以下代码重复
				intent = new Intent().setClass(this, AlbumsActivity.class);   
				spec = tabHost.newTabSpec("albums").setIndicator("Albums",                    
						res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(intent);   
				tabHost.addTab(spec);   
				
				intent = new Intent().setClass(this, SongsActivity.class);   
				spec = tabHost.newTabSpec("songs").setIndicator("Songs", 
						res.getDrawable(R.drawable.ic_tab_artists_white)).setContent(intent);    
				tabHost.addTab(spec);    
				tabHost.setCurrentTab(2);
	}


 

 

实例化3个 Activity

 

 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  TextView textView=new TextView(this);
  textView.setText("This is the Albums tab");//另外两个修改内容
  setContentView(textView); //用View来填充activity内容
 }



最后效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值