package com.yang.tabhost;
import com.yang.tabhost.R;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class ChildTabHostActivity extends TabActivity {
private TabHost tabHost;
private TabWidget tabWidget;
// Tasks.
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// No Title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.child_main_layout);
// 更新状态信息
makeTab();
}
public void makeTab() {
if (this.tabHost == null) {
tabHost = getTabHost();
tabWidget = getTabWidget();
tabHost.setup();
tabHost.bringToFront();
setupLF();
setupQB();
setupZL();
}
for (int i = 0; i < tabWidget.getChildCount(); i++) {
TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(
android.R.id.title);
android.widget.RelativeLayout.LayoutParams tvp = new android.widget.RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tvp.addRule(RelativeLayout.CENTER_HORIZONTAL);
//设置距离顶部6dip
tvp.setMargins(0, 6, 0, 0);
tv.setLayoutParams(tvp);
ImageView iv = (ImageView) tabWidget.getChildAt(i).findViewById(
android.R.id.icon);
android.widget.RelativeLayout.LayoutParams ivp = new android.widget.RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
ivp.addRule(RelativeLayout.CENTER_HORIZONTAL);
ivp.setMargins(0, 6, 0, 0);
iv.setLayoutParams(ivp);
// 得到每个tab
View vvv = tabWidget.getChildAt(i);
// 设置背景色为透明
vvv.setBackgroundColor(Color.TRANSPARENT);
// 设置字体颜色和大小
tv.setTextColor(this.getResources()
.getColorStateList(R.color.white));
tv.setTextSize(15);
}
}
private void setupLF() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.setClass(this, OneActivity.class);
String name = (String) this.getResources().getText(R.string.lf);
tabHost.addTab(tabHost
.newTabSpec(name)
.setIndicator(name,
getResources().getDrawable(R.drawable.tab_selected))
.setContent(intent));
}
private void setupZL() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.setClass(this, TwoActivity.class);
String name = (String) this.getResources().getText(R.string.zl);
tabHost.addTab(tabHost
.newTabSpec(name)
.setIndicator(name,
getResources().getDrawable(R.drawable.tab_selected))
.setContent(intent));
}
private void setupQB() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.setClass(this, ThreeActivity.class);
String name = (String) this.getResources().getText(R.string.qb);
tabHost.addTab(tabHost.newTabSpec(name)
.setIndicator(name, getResources().getDrawable(R.drawable.tab_selected))
.setContent(intent));
}
}
评论16
最新资源