我在动态改变选项卡选项的背景,文本颜色,图标时有一定机率无作用,找了好久找不出原因,求指点。实现方式是:将选项卡的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]);
}
选项卡布局:<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]);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货