Android 动态创建Tab

我们在开发Android程序的时候,会经常使用Tab界面,但有些时候我们需要动态创建Tab页,这里就简单介绍创建Tab的方法。

第一步 界面格局设定

<?xml version="1.0" encoding="utf-8"?>
	<TabHost
	android:layout_width="fill_parent"
	android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/tabhost">
	<TabWidget
	android:id="@android:id/tabs"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
	</TabWidget>
	<FrameLayout
	android:id="@android:id/tabcontent"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:paddingTop="65px">
	<LinearLayout
	android:id="@+id/content1"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView
	android:text="tab1"
	android:id="@+id/phone"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
	</TextView>
	</LinearLayout>
	<LinearLayout
	android:id="@+id/content2"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView
	android:text="tab2"
	android:id="@+id/mail"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
	</TextView>
	</LinearLayout>
	</FrameLayout>

	</TabHost> 
第二步 编写TabActivity的代码
package eoe.stu.ui.tabactivity;

	import android.app.TabActivity;
	import android.os.Bundle;
	import android.widget.TabHost;
	import android.widget.TabHost.TabSpec;

	public class AndroidEggTabActivity extends TabActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

	TabHost tabs = getTabHost();
	TabSpec tab1 = tabs.newTabSpec("tab1");
	tab1.setIndicator("Android");
	tab1.setContent(R.id.content1);
	tabs.addTab(tab1);

	TabSpec tab2 = tabs.newTabSpec("tab2");
	tab2.setIndicator("Egg");
	tab2.setContent(R.id.content2);
	tabs.addTab(tab2);

	tabs.setCurrentTab(0);

	}
	}

   \  

 

   \  


 


转载:http://www.adobex.com/android/source/details/00000427.htm

转载于:https://my.oschina.net/androidcode/blog/105097

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值