尽管google已经废弃了TabActivity..但是,用还是可以用的。话不多说,看代码
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
/**
* Created by Administrator on 2016/8/18.
*/
public class MainTabActivity extends TabActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab_activity);
// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Resources res = getResources();
final TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
//HomeActivity
intent = new Intent().setClass(this, HomeActivity.class);
spec = tabHost.newTabSpec("home").setIndicator(generateView("首
页",R.drawable.tab_dashboard_selector)).setContent(intent);
tabHost.addTab(spec);
//CarControlActivity
intent = new Intent().setClass(this, ControlActivity.class);
spec = tabHost.newTabSpec("contrl").setIndicator(generateView("控
制",R.drawable.tab_control_selector)).setContent(intent);
tabHost.addTab(spec);
//MapActivity
intent = new Intent().setClass(this, NavActivity.class);
spec = tabHost.newTabSpec("suiyi").setIndicator(generateView("随意",
R.drawable.tab_nav_selector)).setContent(intent);
tabHost.addTab(spec);
//SettingActivity
intent = new Intent().setClass(this, SettingActivity.class);
spec = tabHost.newTabSpec("set").setIndicator(generateView("设置",
R.drawable.tab_mine_selector)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById
(R.id.name);
if (tabHost.getCurrentTab() == i) {//选中
tv.setTextColor(MainTabActivity.this.getResources().getColor
(R.color.home_text_color_blue));
} else {//不选中
tv.setTextColor(MainTabActivity.this.getResources().getColor
(R.color.home_text_color_gray));
}
}
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String s) {
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
TextView tv = (TextView) tabHost.getTabWidget().getChildAt
(i).findViewById(R.id.name);
if (tabHost.getCurrentTab() == i) {//选中
tv.setTextColor(MainTabActivity.this.getResources().getColor
(R.color.home_text_color_blue));
} else {//不选中
tv.setTextColor(MainTabActivity.this.getResources().getColor
(R.color.home_text_color_gray));
}
}
}
});
}
/**
* 生成每一个tab
* @param title
* @param icon
* @return
*/
private View generateView(String title, int icon){
LinearLayout singleTab = (LinearLayout) LayoutInflater.from(this).inflate
(R.layout.single_tab, null);
ImageView imgView = (ImageView)singleTab.findViewById(R.id.icon);
imgView.setBackgroundResource(icon);
textView = (TextView)singleTab.findViewById(R.id.name);
textView.setText(title);
textView.setTextColor(this.getResources().getColor(R.color.home_text_color_gray));
textView.setTextSize(12);
return singleTab;
}
}
以下为
single_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"/>
</LinearLayout>
</LinearLayout>
主布局通用
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="@color/tab_bottom_bar"
/>
</LinearLayout>
</TabHost>