在 其他页面修改TabHost 切换选项卡,有时改变选项卡文本颜色无效

sbyniwwg 2013-05-27 02:40:21
我在动态改变选项卡选项的背景,文本颜色,图标时有一定机率无作用,找了好久找不出原因,求指点。实现方式是:将选项卡的view 放到application的数组里面,动态改变时传相应的索引过来重新设定选项的背景,文字颜色等。下面是主要代码。
选项卡布局:

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

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/index_nav_bg">

<LinearLayout
android:id="@+id/channel1"
android:background="@drawable/index_nav_curr"
style="@style/index_tab_but_linear" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/index_nav_main_off" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/orange"
android:text="首页" />
</LinearLayout>

<LinearLayout
android:id="@+id/channel2"
style="@style/index_tab_but_linear" >

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/index_nav_search_off" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="搜索"
/>
</LinearLayout>

<LinearLayout
android:id="@+id/channel3"
style="@style/index_tab_but_linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >


<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/index_nav_cart_off" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购物篮" />
</LinearLayout>

<LinearLayout
android:id="@+id/channel4"
style="@style/index_tab_but_linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/index_nav_cat_off" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="分类" />
</LinearLayout>

<LinearLayout
android:id="@+id/channel5"
style="@style/index_tab_but_linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/index_nav_my_off" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="个人中心" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

</TabHost>


在选项卡的.java文件夹里将TextView 放到application 的一个数组里:
mApplication.getTabTxtArr().add(mCateText1);
mApplication.getTabTxtArr().add(mCateText2);
mApplication.getTabTxtArr().add(mCateText3);
mApplication.getTabTxtArr().add(mCateText4);
mApplication.getTabTxtArr().add(mCateText5);


动态改变选项卡项方法:

public void setIndexTab(int index)
{
int COLOR1 = this.getApplicationContext().getResources().getColor(R.color.index_bottom_menu_off);
int COLOR2 = this.getApplicationContext().getResources().getColor(R.color.index_bottom_menu_on);

TabHost mTabHost = this.getOnlyTabHost();
ArrayList<ImageView> tabImgArr = this.getTabImgArr();
ArrayList<TextView> tabTxtArr = this.getTabTxtArr();
ArrayList<LinearLayout> tabLinLayArr = this.getTabLinLayArr();

int[] nImg = new int[]{R.drawable.index_nav_main_off,R.drawable.index_nav_search_off,R.drawable.index_nav_cart_off,R.drawable.index_nav_cat_off,R.drawable.index_nav_my_off};
int[] cImg = new int[]{R.drawable.index_nav_main_on,R.drawable.index_nav_search_on,R.drawable.index_nav_cart_on,R.drawable.index_nav_cat_on,R.drawable.index_nav_my_on};
int[] channel = new int[]{R.id.channel1,R.id.channel2,R.id.channel3,R.id.channel4,R.id.channel5};


//将选项卡全部选项还原为未选中的颜色,背景,图标 和设置选中项的颜色,背景,图标 问题就在这里有一定的机率这里重设时无效果,但我在循环里输出却能看到相应的文本,如我输出:log.d('tab',tabTxtArr.get(i).getText()) 会设选项卡的五个文本输出,但重新设置的颜色等却无效果
for(int i=0;i<tabImgArr.size();i++)
{
tabImgArr.get(i).setImageResource(nImg[i]);
tabTxtArr.get(i).setTextColor(COLOR1);
tabLinLayArr.get(i).setBackgroundResource(R.drawable.none);
}

tabTxtArr.get(index).setTextColor(COLOR2);
tabImgArr.get(index).setImageResource(cImg[index]);
tabLinLayArr.get(index).setBackgroundResource(R.drawable.index_nav_curr);



mTabHost.setCurrentTab(index);


this.setCurTabId(channel[index]);

...全文
118 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sbyniwwg 2013-05-27
  • 打赏
  • 举报
回复
问题解决了。 每次问题问题基本上都是自己回答,自己解决。
sbyniwwg 2013-05-27
  • 打赏
  • 举报
回复
在每次重新安装时,即在每次eclipse 重新run 时就不会出现这问题,但退出程序号再打开问题就出来了

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧