zoukankan      html  css  js  c++  java
  • (更新TabHost图标) Update TabHost Icon

    我们在使用Tabhost的时候,有时候需要动态更改icon图标,可以用两种方式。

    第一种在java代码中实现

    代码
    1 public void onTabChanged(String tabId) {
    2 if(tabId.equals("checkin")){
    3 ImageView iv = (ImageView) getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
    4 iv.setImageDrawable(getResources().getDrawable(R.drawable.friends_tab));
    5 iv = (ImageView) getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
    6 iv.setImageDrawable(getResources().getDrawable(R.drawable.map_tab_unselected));
    7 iv = (ImageView) getTabWidget().getChildAt(2).findViewById(android.R.id.icon);
    8 iv.setImageDrawable(getResources().getDrawable(R.drawable.tips_tab_unselected));
    9 }else if(tabId.equals("map")){
    10 ImageView iv = (ImageView) getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
    11 iv.setImageDrawable(getResources().getDrawable(R.drawable.friends_tab_unselected));
    12 iv = (ImageView) getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
    13 iv.setImageDrawable(getResources().getDrawable(R.drawable.map_tab_selected));
    14 iv = (ImageView) getTabWidget().getChildAt(2).findViewById(android.R.id.icon);
    15 iv.setImageDrawable(getResources().getDrawable(R.drawable.tips_tab_unselected));
    16 } else{
    17 ImageView iv = (ImageView) getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
    18 iv.setImageDrawable(getResources().getDrawable(R.drawable.friends_tab_unselected));
    19 iv = (ImageView) getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
    20 iv.setImageDrawable(getResources().getDrawable(R.drawable.map_tab_unselected));
    21 iv = (ImageView) getTabWidget().getChildAt(2).findViewById(android.R.id.icon);
    22 iv.setImageDrawable(getResources().getDrawable(R.drawable.tips_tab_selected));
    23 }
    24
    25 }

    The short answer is, you're not missing anything. The Android SDK doesn't provide a direct method to change the indicator of a TabHost after it's been created. The TabSpec is only used to build the tab, so changing the TabSpec after the fact will have no effect.

    I think there's a workaround, though. Call mTabs.getTabWidget() to get a TabWidget object. This is just a subclass of ViewGroup, so you can call getChildCount() and getChildAt() to access individual tabs within the TabWidget. Each of these tabs is also a View,

    and in the case of a tab with a graphical indicator and a text label, it's almost certainly some other ViewGroup (maybe a LinearLayout, but it doesn't matter) that contains an ImageView and a TextView. So with a little fiddling with the debugger or Log.i, you should be able to figure out a recipe to get the ImageView and change it directly.

    The downside is that if you're not careful, the exact layout of the controls within a tab could change and your app could break. Your initial solution is perhaps more robust, but then again it might lead to other unwanted side effects like flicker or focus problems.

    第二种使用xml文件,如例:
     先在xml文件中定义好一个selector
    代码
    1 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- Non focused states -->
     <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
     <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />
     <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> <!-- Pressed -->
    <item android:state_pressed="true" android:drawable="@drawable/tab_press" />
    </selector>
    两种方式都可以的,不过我只测试过用第一种方式。两种方式的性能方面我想应该是第二种方式更优.
  • 相关阅读:
    Lua获取当前时间
    标准库
    Cocos2d-x retain和release倒底怎么玩?
    lua 中处理cocos2dx 的button 事件
    探讨把一个元素从它所在的div 拖动到另一个div内的实现方法
    从 ie10浏览器下Symbol 未定义的问题 探索vue项目如何兼容ie低版本浏览器(ie9, ie10, ie 11 )
    setTimeout里无法调用鼠标事件的event
    浅谈HTTP缓存以及后端,前端如何具体实现HTTP缓存
    window7电脑git设置快捷命令
    从获取点击事件根元素谈 target和currentTarget
  • 原文地址:https://www.cnblogs.com/enricozhang/p/1743742.html
Copyright © 2011-2022 走看看