Android之TabHost改变Tab颜色

在Android的开发中我们经常需要用到TabHost,其中的一个效果就是用户点击之后Tab切换到相应的颜色,点击该Tab,该Tab就变为点击后的颜色,再次点击其他Tab,该tab才会变回未点击的颜色,接下来我们就给出代码的实现。

1:要想用TabHost,xml文件中一定要指定为特定的格式。

<?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">
    <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
            <FrameLayout android:layout_width="fill_parent"
                         android:layout_height="0dp"
                         android:layout_weight="1">
                <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="#f0f0f0"/>
                <View
                        android:layout_width="fill_parent"
                        android:layout_height="4dp"
                        android:layout_gravity="bottom"
                        android:background="@drawable/shadow_bottom"/>
            </FrameLayout>
            <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
					android:background="#303030"
					android:divider="@null"
                    android:fadingEdge="none"
                    android:fadingEdgeLength="0.0px"
                    android:paddingLeft="0dp"
                    android:paddingRight="0dp"/>
        </LinearLayout>
    </FrameLayout>
</TabHost>

2:xml中指定特定的格式后还不够,我们的Activity还要继承自TabActivity,这样通过在程序总调用getTabHost()方法,我们就可以得到TabHost控件了。为了在界面的底部显示一横排的Tab选项,我们需要为TabHost添加TabHost.TabSpec,通过查看源代码可以得知可以为TabHost.TabSpec设置View,设置tag,以及设置intent,从而达到点击不同的tab选项来进行不同的界面切换。下面给出代码,里面带有详细的注释。

package com.xy.tabhosttabitemchangebackground;

import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.*;

public class ActivityMain extends TabActivity implements TabHost.OnTabChangeListener {
	public static final String kTagOne = "tag_one";//tab1的tag标记
	public static final String kTagTwo = "tag_two";//tab2的tag标记
	public static final String kTabThree = "tag_three";//tab3的tag标记
	public static final String kTagFour = "tag_four";//tab4的tag标记
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getTabHost().setOnTabChangedListener(this);//为TabHost添加监听
		initViews();//初始化TabHost底部选项卡
	}

	private void initViews(){
		addTab(R.drawable.tab_icon_book_list_selector, kTagOne,ActivityOne.class, "tag1");
		addTab(R.drawable.tab_icon_ranks_selector, kTagTwo,ActivityTwo.class, "tag2");
		addTab(R.drawable.tab_icon_discovery_selector, kTabThree,ActivityThree.class, "tag3");
		addTab(R.drawable.tab_icon_mine_selector, kTagFour,ActivityFour.class, "tag4");
		changeTabBackGround();//选项卡添加完毕后,更改当前选项卡的背景颜色
	}
	
	//参数分别为:图片背景id,tag标记,点击不同的选项卡要切换的Activity,tab描述文字
	protected void addTab(int tabIconResId, String tag, Class<?> tabActivity,String text) {
		TabHost tab_host = this.getTabHost();//得到当前Activity的TabHost
		//通过inflate初始化View布局
		View tab_page = View.inflate(this, R.layout.view_tab_page, null);
		//为View中ImageView设置背景
		((ImageView) tab_page.findViewById(R.id.tab_icon)).setImageResource(tabIconResId);
		//为View中TextView设置文字
		((TextView) tab_page.findViewById(R.id.tab_text)).setText(text);
		//初始化Intent
		Intent intent = new Intent(this, tabActivity);
		//构建Tab选项卡,即TabHost.TabSpec
		TabHost.TabSpec spec = tab_host.newTabSpec(tag).setIndicator(tab_page).setContent(intent);
		tab_host.addTab(spec);//将选项添加进TabHost中
	}
	
	//点击某一个选项卡切换该选项卡背景的方法
	private void changeTabBackGround(){
		//得到当前选中选项卡的索引
		int index = getTabHost().getCurrentTab();
		//调用tabhost中的getTabWidget()方法得到TabWidget
		TabWidget tabWidget = getTabHost().getTabWidget();
		//得到选项卡的数量
		int count = tabWidget.getChildCount();
		//循环判断,只有点中的索引值改变背景颜色,其他的则恢复未选中的颜色
		for(int i=0;i<count;i++){
			View view = tabWidget.getChildAt(i);
			if(index == i){
				view.setBackgroundResource(R.color.main_tab_pressed_bg);
			}else{
				view.setBackgroundResource(Color.TRANSPARENT);
			}
		}
	}
	
	//选项卡切换监听
	@Override
	public void onTabChanged(String arg0) {
		changeTabBackGround();
	}
}

3:以上方法可以切换选中卡片的背景颜色,还有另外一种方法可以切换选中项的背景颜色,下面给出代码,这两种方法本质上是没有区别的,只不过区别为:一个为系统管理我们创建的View,一个为我们自己管理创建的View,通过查看源代码可知:TabHost.TabSpec中持有我们创建view的引用,并且为final的,所以指向的是同一个View。下面给出代码,注释已经详细给出。

package com.xy.tabhosttabitemchangebackgroundother;

import java.util.HashMap;
import java.util.Set;

import com.xy.tabhosttabitemchangebackgroundother.R;

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

public class ActivityMain extends TabActivity implements TabHost.OnTabChangeListener {
	public static final String kTagOne = "tag_one";
	public static final String kTagTwo = "tag_two";
	public static final String kTabThree = "tag_three";
	public static final String kTagFour = "tag_four";
	//创建Map用来存储创建的View
	private HashMap<String,View> tabs = new HashMap<String,View>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getTabHost().setOnTabChangedListener(this);
		initViews();
	}

	private void initViews(){
		addTab(R.drawable.tab_icon_book_list_selector, kTagOne,ActivityOne.class, "tag1");
		addTab(R.drawable.tab_icon_ranks_selector, kTagTwo,ActivityTwo.class, "tag2");
		addTab(R.drawable.tab_icon_discovery_selector, kTabThree,ActivityThree.class, "tag3");
		addTab(R.drawable.tab_icon_mine_selector, kTagFour,ActivityFour.class, "tag4");
		changeTabBackGround(kTagOne);
	}
	
	protected void addTab(int tabIconResId, String tag, Class<?> tabActivity,String text) {
		TabHost tab_host = this.getTabHost();
		View tab_page = View.inflate(this, R.layout.view_tab_page, null);
		((ImageView) tab_page.findViewById(R.id.tab_icon)).setImageResource(tabIconResId);
		((TextView) tab_page.findViewById(R.id.tab_text)).setText(text);
		Intent intent = new Intent(this, tabActivity);
		TabHost.TabSpec spec = tab_host.newTabSpec(tag).setIndicator(tab_page).setContent(intent);
		//将创建的View添加进Map
		tabs.put(tag, tab_page);
		tab_host.addTab(spec);
	}
	
	//改变选项卡背景颜色的方法
	private void changeTabBackGround(String tagId){
		//得到key的集合
		Set<String> tags = tabs.keySet();
		//循环判断,点中了指定tag的View要更换颜色
		for(String tag:tags){
			if(tag.equals(tagId)){
				tabs.get(tag).setBackgroundResource(R.color.main_tab_pressed_bg);
			}else{
				tabs.get(tag).setBackgroundResource(R.color.color_of_00000000);
			}
		}
	}
	
	//系统回调的方法中参数为TabHost.Tabspec中调用setIndicator()方法设置的tag
	@Override
	public void onTabChanged(String tagId) {
		changeTabBackGround(tagId);
	}
}

4:ok,以上为tabhost的小例子,下面给出下载地址。

http://download.csdn.net/detail/ly985557461/7274693

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabHostAndroid 应用程序中常用的选项卡控件,用于在多个选项卡之间切换。以下是 Android Studio 中使用 TabHost 控件的步骤: 1. 在布局文件中添加 TabHost 控件 ``` <TabHost android:id="@+id/tab_host" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 添加选项卡内容布局 --> <LinearLayout android:id="@+id/tab1_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab 1" /> </LinearLayout> <LinearLayout android:id="@+id/tab2_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab 2" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> ``` 2. 在 Java 代码中获取 TabHost 控件并设置选项卡 ```java TabHost tabHost = findViewById(R.id.tab_host); tabHost.setup(); // 添加选项卡 TabHost.TabSpec tab1 = tabHost.newTabSpec("Tab1"); tab1.setIndicator("Tab 1"); tab1.setContent(R.id.tab1_layout); tabHost.addTab(tab1); TabHost.TabSpec tab2 = tabHost.newTabSpec("Tab2"); tab2.setIndicator("Tab 2"); tab2.setContent(R.id.tab2_layout); tabHost.addTab(tab2); ``` 在以上代码中,我们首先获取 TabHost 控件,并通过 `setup()` 方法初始化。然后,我们使用 `newTabSpec()` 方法创建选项卡,并设置选项卡的标签和内容布局。最后,我们使用 `addTab()` 方法将选项卡添加到 TabHost 控件中。 以上就是使用 TabHost 控件的基本步骤。你可以根据自己的需要自定义选项卡的样式和内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值