TabHost的使用及Tabwidget的颜色设置

</pre><p>下面是用TabHost组件实现一个带底部导航栏的界面。</p><p></p><pre name="code" class="java">package com.Jim.tabhostdemo;

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

public class MainActivity extends TabActivity implements OnTabChangeListener {

	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去除标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		// 初始化tabhost
		tabHost = getTabHost();
	
		TabSpec tabSpec1 = tabHost.newTabSpec("tag1")
				//设置tabwidget的显示内容,此处还可以加入自定义view等
				.setIndicator("游戏")
				//设置tabwidget对应页面的显示内容
				.setContent(new Intent(MainActivity.this, FirstActivity.class));
		//添加第一个页面到tabhost中
		tabHost.addTab(tabSpec1);
		
		TabSpec tabSpec2 = tabHost.newTabSpec("tag2")
				.setIndicator("音乐")
				.setContent(new Intent(MainActivity.this, SecondActivity.class));
		tabHost.addTab(tabSpec2);
		
		TabSpec tabSpec3 = tabHost.newTabSpec("tag3")
				.setIndicator("体育")
				.setContent(new Intent(MainActivity.this, ThirdActivity.class));
		tabHost.addTab(tabSpec3);

		// 初始化tabwidget
		updateTab(tabHost);
		// 设置事件监听
		tabHost.setOnTabChangedListener(this);
	}

	private void updateTab(TabHost tabHost) {
		for (int i = 0; i < 3; i++) {
			
			View view = tabHost.getTabWidget().getChildAt(i);
			if (tabHost.getCurrentTab() == i) {
				//设置tabwidget选中时的背景颜色
				view.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.red));
			} else {
				//设置未选中的背景颜色
				view.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.grary));
			}
		}
	}

	@Override
	public void onTabChanged(String tabId) {
		// tabwidget的事件监听
		updateTab(tabHost);
	}

}

主XML文件如下:
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            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"
                android:layout_weight="1" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>
            </FrameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="false" >
            </TabWidget>
        </LinearLayout>
    </TabHost>

</LinearLayout>


 
效果如下:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值