改变 TabHost 默认样式的高度、字体
1.创建android项目tabHostDemo,在包com.yinlans下创建MainActivity.java对应的布局文件main.xml
2.main.xml布局文件代码如下:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/framelayout01″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<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”>
<!–注意TabWidget的 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_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″>
<LinearLayout
android:id=”@+id/llayout1″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”>
<ListView
android:id=”@+id/listview1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”></ListView>
</LinearLayout>
<LinearLayout
android:id=”@+id/llayout2″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”>
<ListView
android:id=”@+id/listview2″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”></ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
3.MainActivity.java代码如下:
package com.yinlans;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉主页的标题
setContentView(R.layout.main);
TabHost tabHost=(TabHost) this.findViewById(R.id.tabhost);
tabHost.setup();
TabWidget tabWidget=tabHost.getTabWidget();
tabHost.addTab(tabHost.newTabSpec(“all”).setContent(R.id.llayout1).setIndicator(“全部相薄”));
tabHost.addTab(tabHost.newTabSpec(“we”).setContent(R.id.llayout2).setIndicator(“我的收藏”));
//注意 这个就是改变TabHost默认样式,必须放在上面这段代码的下面,不然改变不了
for(int i=0;i<tabWidget.getChildCount();i++){
tabWidget.getChildAt(i).getLayoutParams().height=50;
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));
}
}
}
No Responses To This Post So Far(Rss)