关于TabActivity相关使用

  尽管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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值