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>
    两种方式都可以的,不过我只测试过用第一种方式。两种方式的性能方面我想应该是第二种方式更优.
  • 相关阅读:
    WebQQ2.0 PHP
    HTML文档类型 PHP
    字符●圆角 PHP
    IIS日志分析器 PHP
    JS 像素数字 PHP
    3DTagCloud3D标签云 PHP
    QQ截屏工具提取 PHP
    .NET嵌入DLL ILMerge工具应用 PHP
    JS CSS 压缩工具(GUI界面) PHP
    Javascript 函数初探
  • 原文地址:https://www.cnblogs.com/enricozhang/p/1743742.html
Copyright © 2011-2022 走看看