TabActivity自定义底部导航栏

TabActivity本身继承自ActivityGroup,主要功能是实现多个activity或者view之间的切换和显示,要使用该类必须新建一个类来继承TabActivity,并且该类的xml配置文件中必须包含、、三个视图(View),其中后面两个标签是前面一个标签的子标签,表示tab页的选项卡,相当于菜单页,表示显示内容的区域。

1.自定义底部导航栏布局

由于TabWidget不方便自定义布局,因此在布局中将其隐藏,然后自己写布局代替TabWidget效果。
注意TabHost TabWidget FrameLayout的id必须是固定的,不能自己定义Id

<?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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <LinearLayout
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:background="#ffffff"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/tab_nearby"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/iv_nearby"
                    android:layout_width="25dp"
                    android:layout_height="20dp"
                    android:background="@drawable/nearby_select"
                    android:clickable="false" />

                <TextView
                    android:id="@+id/tv_nearby"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:text="附近"
                    android:textColor="@color/text30"
                    android:textSize="12sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab_message"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/iv_message"
                    android:layout_width="25dp"
                    android:layout_height="20dp"
                    android:background="@drawable/message_no"
                    android:clickable="false" />

                <TextView
                    android:id="@+id/tv_message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:text="消息"
                    android:textColor="@color/text30"
                    android:textSize="12sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab_home"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/iv_home"
                    android:layout_width="25dp"
                    android:layout_height="20dp"
                    android:background="@drawable/home_no"
                    android:clickable="false" />

                <TextView
                    android:id="@+id/tv_home"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:text="首页"
                    android:textColor="@color/text30"
                    android:textSize="12sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab_find"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/iv_find"
                    android:layout_width="25dp"
                    android:layout_height="20dp"
                    android:background="@drawable/find_no"
                    android:clickable="false" />

                <TextView
                    android:id="@+id/tv_find"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:text="发现"
                    android:textColor="@color/text30"
                    android:textSize="12sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab_user"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/iv_user"
                    android:layout_width="25dp"
                    android:layout_height="20dp"
                    android:background="@drawable/user_no"
                    android:clickable="false" />

                <TextView
                    android:id="@+id/tv_user"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:text="我的"
                    android:textColor="@color/text30"
                    android:textSize="12sp" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
</TabHost>

2.TabActivity使用

public class MainActivity extends TabActivity implements OnClickListener {
    private TabHost tabhost;
    public static final String TAB_NEAR = "tabNearby";
    public static final String TAB_MESSAGE = "tabMessage";
    public static final String TAB_HOME = "tabHome";
    public static final String TAB_FIND = "tabFind";
    public static final String TAB_USER = "tabUser";
    private LinearLayout li_tab_nearby, li_tab_message, li_tab_home,
            li_tab_find, li_tab_user;
    private ImageView img_nearby, img_message, img_home, img_find, img_user;
    private TextView tv_nearby, tv_message, tv_home, tv_find, tv_user;
    private Activity ac;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ac = this;
        initView();
        initEvent();
        //重置底部导航拦
        resetTab();
        //默认选中第一个选项卡
        selectTab(1);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    /**
     * 重置底部导航栏,由于每次点击效果都要改变,先重置成默认的,然后把点击的特殊处理
     */
    private void resetTab() {
        // TODO Auto-generated method stub
        img_nearby.setBackgroundResource(R.drawable.nearby_no);
        img_message.setBackgroundResource(R.drawable.message_no);
        img_home.setBackgroundResource(R.drawable.home_no);
        img_find.setBackgroundResource(R.drawable.find_no);
        img_user.setBackgroundResource(R.drawable.user_no);

        tv_nearby.setTextColor(getResources().getColor(R.color.text30));
        tv_message.setTextColor(getResources().getColor(R.color.text30));
        tv_home.setTextColor(getResources().getColor(R.color.text30));
        tv_find.setTextColor(getResources().getColor(R.color.text30));
        tv_user.setTextColor(getResources().getColor(R.color.text30));

    }

    private void initEvent() {
        // TODO Auto-generated method stub
        li_tab_nearby.setOnClickListener(this);
        li_tab_message.setOnClickListener(this);
        li_tab_home.setOnClickListener(this);
        li_tab_find.setOnClickListener(this);
        li_tab_user.setOnClickListener(this);
    }

    private void initView() {
        // TODO Auto-generated method stub
        li_tab_nearby = (LinearLayout) findViewById(R.id.tab_nearby);
        li_tab_message = (LinearLayout) findViewById(R.id.tab_message);
        li_tab_home = (LinearLayout) findViewById(R.id.tab_home);
        li_tab_find = (LinearLayout) findViewById(R.id.tab_find);
        li_tab_user = (LinearLayout) findViewById(R.id.tab_user);

        img_nearby = (ImageView) findViewById(R.id.iv_nearby);
        img_message = (ImageView) findViewById(R.id.iv_message);
        img_home = (ImageView) findViewById(R.id.iv_home);
        img_find = (ImageView) findViewById(R.id.iv_find);
        img_user = (ImageView) findViewById(R.id.iv_user);

        tv_nearby = (TextView) findViewById(R.id.tv_nearby);
        tv_message = (TextView) findViewById(R.id.tv_message);
        tv_home = (TextView) findViewById(R.id.tv_home);
        tv_find = (TextView) findViewById(R.id.tv_find);
        tv_user = (TextView) findViewById(R.id.tv_user);

        tabhost = getTabHost();
        tabhost.addTab(tabhost.newTabSpec(TAB_NEAR).setIndicator(TAB_NEAR)
                .setContent(new Intent(this, NearbyActivity.class)));
        tabhost.addTab(tabhost.newTabSpec(TAB_MESSAGE)
                .setIndicator(TAB_MESSAGE)
                .setContent(new Intent(this, MessageActivity.class)));
        tabhost.addTab(tabhost.newTabSpec(TAB_HOME).setIndicator(TAB_HOME)
                .setContent(new Intent(this, HomeActivity.class)));
        tabhost.addTab(tabhost.newTabSpec(TAB_FIND).setIndicator(TAB_FIND)
                .setContent(new Intent(this, FindActivity.class)));
        tabhost.addTab(tabhost.newTabSpec(TAB_USER).setIndicator(TAB_USER)
                .setContent(new Intent(this, UserActivity.class)));
    }

    /**
     * 选项卡的选择,同时把选中的选项卡对应的导航栏做特殊处理
     */
    private void selectTab(int i) {
        switch (i) {
            case 1:
                resetTab();
                tabhost.setCurrentTabByTag(TAB_NEAR);
                img_nearby.setBackgroundResource(R.drawable.nearby_select);
                tv_nearby
                        .setTextColor(getResources().getColor(R.color.user_heart1));
                break;
            case 2:
                resetTab();
                tabhost.setCurrentTabByTag(TAB_MESSAGE);
                img_message.setBackgroundResource(R.drawable.message_select);
                tv_message.setTextColor(getResources()
                        .getColor(R.color.user_heart1));
                break;
            case 3:
                resetTab();
                tabhost.setCurrentTabByTag(TAB_HOME);
                img_home.setBackgroundResource(R.drawable.home_select);
                tv_home.setTextColor(getResources().getColor(R.color.user_heart1));
                break;
            case 4:
                resetTab();
                tabhost.setCurrentTabByTag(TAB_FIND);
                img_find.setBackgroundResource(R.drawable.find_select);
                tv_find.setTextColor(getResources().getColor(R.color.user_heart1));
                break;
            case 5:
                resetTab();
                tabhost.setCurrentTabByTag(TAB_USER);
                img_user.setBackgroundResource(R.drawable.user_select);
                tv_user.setTextColor(getResources().getColor(R.color.user_heart1));
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.tab_nearby:
                selectTab(1);
                break;
            case R.id.tab_message:
                selectTab(2);
                break;
            case R.id.tab_home:
                selectTab(3);
                break;
            case R.id.tab_find:
                selectTab(4);
                break;
            case R.id.tab_user:
                selectTab(5);
                break;
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值