开发者

Customizing TabWidget in SDK 1.5, API 3

I'm aware that API 3 doesn't allow a view to be set for a tab, but I still need to modify the TextView displayed as the indicator. I'd also like to change the Drawable for the tab, but I don't see a way to do it without a custom tab view as allowed in 1.6, API 4.

Working with this generic example below, is there way to retrieve the TextView and modify its properties or change the drawable?

       TabHost tabHost = getTabHost();
       TabHost.TabSpec spec;

       spec = tabHost.newTabSpec("nearby")
                      .setIndicator("Nearby Activity")
                      .setContent(R.id.nearby_list);                
       tabHost.addTab(spec);        

       spec = tabHost.newTabSpec("friends")
                  .setIndicator("Friends & Favorites")
                      .setContent(R.id.friends_list);
       tabHost.addTab(spec);        

       tabHost.setCurren开发者_JS百科tTabByTag("nearby");

Thanks for any help.


to change the icon:

 .setIndicator("Friends & Favorites",getResources().getDrawable(R.drawable.myTabIcon))

to change the TabBackground Drawable::

tabHost.getTabWidget().getChildAt( index of tab ).setBackgroundResource(R.drawable.mytab_Newbackground);

to retrieve the Textview, check Francesco's sample

ViewGroup v = (ViewGroup) tabHost.getTabWidget().getChildAt( index of tab );
TextView tv = (TextView)v.getChildAt(1); 
Log.d("Tab's text = ",tv.getText().toString());


Since the Tabwidget is a normal ViewGroup I think you should access to the all its children. Something like this should work:

   tabHost.setOnTabChangedListener(this);
....
...


@Override
    public void onTabChanged(String tabId) {

        int count = tabHost.getTabWidget().getChildCount();
        for (int i = 0; i < count; i++){
            ViewGroup v = (ViewGroup) tabHost.getTabWidget().getChildAt(i); //Should be a relative layout
            TextView tv = (TextView)v.getChildAt(1); //At position 0 should be the ImageView
            Log.d("onTabChanged",tv.getText().toString());
        }

    }

Maybe, you have to play with the indexes position to find the right View.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜