android自定义TabHost经典案例
TabHost案例
1、实现效果
2、实现原理
本实例将实现对TabHost控件的应用,实现原理有:
自定义TabSpace标签
将已经存在的其它Activity窗口当作TabHost的内容显示
3、实现步骤
第一步:创建项目
说明:这里创建一个android项目,名称改了TabHostTestActivity
包名设置为:
第二步:设置布局文档(main.xml)
具体代码如下:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@android:id/tabs"
android:layout_width="105dp"
android:layout_height="fill_parent"
android:background="#cccccc"/>
android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff"
android:minHeight="365dip" >
第三步:编写Activity窗口代码
(1)创建全局对象
private TabHost tabHost; // 创建TabHost对象
private TabWidget tabWidget; // 创建TabWidget容器对象
public static Context context;private TabSpec Tab1,Tab2,Tab3,Tab4,Tab5,Tab6,Tab7,Tab8;// 标签对象
(2)设置Activity的onCreate方法。
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示系统的标题
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
setContentView(R.layout.main);makeTab(); // 设置Tab控件的自定义方法
}
(3)创建makeTab()方法,实现对Tab控件的定义设置。
public TabHost makeTab() {
if (this.tabHost == null) {
tabHost = (TabHost) findViewById(android.