您的位置:首页 > 移动开发 > Android开发

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

2016-05-20 20:41 567 查看


主要代码

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java android