1.创建android项目TabHost_Demo01,在包android.huada下面创建TabHost_Demo01Activity.java 对应的布局文件main.xml
2.TabHost_Demo01Activity.java 代码如下
package android.huada;
import android.app.Activity; import android.app.TabActivity; import android.os.Bundle; import android.view.View; import android.widget.TabHost; import android.widget.TabWidget; import android.widget.TextView;
public class TabHost_Demo01Activity extends Activity { /** Called when the activity is first created. */ TabWidget tabWidget ; TabHost tabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tabHost = (TabHost) findViewById(R.id.tabhost); tabHost.setup(); tabWidget = tabHost.getTabWidget(); tabHost.addTab(tabHost.newTabSpec("PLAN").setContent(R.id.LinearLayout001) .setIndicator("计划中")); tabHost.addTab(tabHost.newTabSpec("COMPLTED").setContent(R.id.LinearLayout003) .setIndicator("已完成")); // 注意这个就是改变Tabhost默认样式的地方,一定将这部分代码放在上面这段代码的下面,不然样式改变不了 for (int i =0; i < tabWidget.getChildCount(); i++) { tabWidget.getChildAt(i).getLayoutParams().height = 30; tabWidget.getChildAt(i).getLayoutParams().width = 65; TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title); tv.setTextSize(15); tv.setTextColor(this.getResources().getColorStateList(android.R.color.white)); } } }
3. main.xml布局文件代码如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <!-- 这里面注意android:layout_height="wrap_content" --> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"> <LinearLayout android:id="@+id/LinearLayout001" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/ListView1" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> <!-- <LinearLayout android:id="@+id/LinearLayout002" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/ListView2" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> --> <LinearLayout android:id="@+id/LinearLayout003" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/ListView3" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
4.运行效果图如下
![TabHost 改变默认样式的高度,字体 - lhl_soft - 程序员](http://img0.ph.126.net/TLcDgMNlSh8_hckVNQ3zTQ==/30962247455407090.jpg)