TabHosts导航按钮实现自定义图片和文字


主要代码

package com.cc.selftabhost;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

/**
 * @author cc
 * 1.TabHostActivity extends TabActivity
 * 2.	TabHost tabhost = getTabHost();
 * 3.需要介个页面就创建几个空的TabActivity,并在manifest注册
 * 4.几个tab导航按钮,就创建几个xml 按钮的布局,比如上边图片下边文字。图片的位置可以使用selector控制焦点获取变换图片
 * 
 * 【5-9 重复添加多个标签】
 * 5.创建标签  TabSpec tabSpec01 = tabhost.newTabSpec("tab1");
 * 6.填充导航按钮的布局 tabSpec01.setIndicator(View.inflate(TabHostMainActivity.this,R.layout.tab_main, null));
 * 7.Intent跳转
 * 8.标签 setContent  意图
 * 9.tabhost.addTab 加载  标签。
 *
 */
public class TabHostMainActivity extends TabActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabhost_main);

		// 继承 tabActivity,得到tabHost
		TabHost tabhost = getTabHost();
		// 定义第一个tab 导航按钮
		TabSpec tabSpec01 = tabhost.newTabSpec("tab1");
		// 设置tab 导航按钮的布局
		tabSpec01.setIndicator(View.inflate(TabHostMainActivity.this,
				R.layout.tab_main, null));
		// 添加意图跳转
		Intent intent1 = new Intent(TabHostMainActivity.this,
				TabMainActivity.class);
		// 设置标题对应的内容
		tabSpec01.setContent(intent1);
		// 添加到tab中
		tabhost.addTab(tabSpec01);

		// =================================================================================================
		TabSpec spec2 = tabhost.newTabSpec("Item2");
		spec2.setIndicator(View.inflate(TabHostMainActivity.this,
				R.layout.tab_class, null));
		Intent intent2 = new Intent(TabHostMainActivity.this, TabClassActivity.class);
		spec2.setContent(intent2);
		tabhost.addTab(spec2);
		// ===================================================================================================
		TabSpec spec3 = tabhost.newTabSpec("Item3");
		spec3.setIndicator(View.inflate(TabHostMainActivity.this,
				R.layout.tab_ludao, null));
		Intent intent3 = new Intent(TabHostMainActivity.this, TabAboutActivity.class);
		spec3.setContent(intent3);
		tabhost.addTab(spec3);
		// =========================================================
		TabSpec spec4 = tabhost.newTabSpec("Item4");
		spec4.setIndicator(View.inflate(TabHostMainActivity.this,
				R.layout.tab_mine, null));
		Intent intent4 = new Intent(TabHostMainActivity.this, TabMineActivity.class);
		spec4.setContent(intent4);
		tabhost.addTab(spec4);

	}

}


<?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" >

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@android:id/tabs"
            android:layout_alignParentTop="true" >
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" >
        </TabWidget>
    </RelativeLayout>

</TabHost>


DEMO下载地址:http://download.csdn.net/detail/flyingsir_zw/9526597



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值