Android之TabHost改变Tab颜色

来源:互联网 发布:虚拟机安装ubuntu出错 编辑:程序博客网 时间:2024/05/17 14:19

在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标记@Overrideprotected 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);//初始化IntentIntent intent = new Intent(this, tabActivity);//构建Tab选项卡,即TabHost.TabSpecTabHost.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()方法得到TabWidgetTabWidget 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);}}}//选项卡切换监听@Overridepublic 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用来存储创建的Viewprivate HashMap<String,View> tabs = new HashMap<String,View>();@Overrideprotected 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添加进Maptabs.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@Overridepublic void onTabChanged(String tagId) {changeTabBackGround(tagId);}}

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

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

0 0
原创粉丝点击