布局总共有两种方法,第一种就是xml布局,还有就是自定义布局
<span style="font-size:18px;"><TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 总布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<!-- 与源代码的id一样 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 这是第一个布局即使首页 -->
<LinearLayout
android:id="@+id/page1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是第一个标签页" >
</TextView>
</LinearLayout>
<!-- 这是第二页 -->
<LinearLayout
android:id="@+id/page2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是第2个标签页" >
</TextView>
</LinearLayout>
<!-- 这是第三页 -->
<LinearLayout
android:id="@+id/page3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是第3个标签页" >
</TextView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost></span>
Activity是
<span style="font-size:14px;">package cc.tabhost;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class MainActivity extends Activity {
private TabHost tabhost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabhost = (TabHost) this.findViewById(R.id.tabhost);
tabhost.setup();// 首先找到tabwidget与TabContent两个控件
TabSpec tabSpec = tabhost.newTabSpec("page1");// 设置标记。以后可以通过该标记找到该页
tabSpec.setIndicator("首页", getResources().getDrawable(R.drawable.i1));// 设置显示的字,与图片
// tabSpec.setIndicator(createTabview("首页"));//自定义布局用的tabSpec.setIndicator(View
// v)
tabSpec.setContent(R.id.page1);
tabhost.addTab(tabSpec);
tabSpec = tabhost.newTabSpec("page2");
tabSpec.setIndicator("第二页", getResources().getDrawable(R.drawable.i2));
// tabSpec.setIndicator(createTabview("第二页"));
tabSpec.setContent(R.id.page2);
tabhost.addTab(tabSpec);
tabSpec = tabhost.newTabSpec("page3");
tabSpec.setIndicator("第三页", getResources().getDrawable(R.drawable.i3));
// tabSpec.setIndicator(createTabview("第三页"));//自定义tabcontent布局
tabSpec.setContent(R.id.page3);
tabhost.addTab(tabSpec);
tabhost.setCurrentTab(0);// 选择停留到第一个页为首页
}
/**
* 自定义布局,
*
* @param 显示的首页
* ,第二页。。
* @return 一个view
*/
private View createTabview(String string) {
View v = getLayoutInflater().inflate(R.layout.tab, null);// 找到自定义的tab布局
TextView tv = (TextView) (v.findViewById(R.id.name));
tv.setText(string);
return v;
}
@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;
}
}
</span>
以上有自定义布局与xml两种方法的代码,以下是自定义布句的tab.xml
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/tab_bg"
android:gravity="center"
android:textSize="18sp" />
</LinearLayout></span>