开发者

How to change title of a Tab in TabActivity

I have TabActivity with tabs on bottom which is composed of several different activities like this:

public class HomeScreen extends TabActivity {
private TabHost mTabHost;
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    try{
    setContentView(R.layout.hometabs);

开发者_高级运维
    Resources res = getResources(); // Resource object to get Drawables
    mTabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab
    String strName;
    // Create an Intent to launch an Activity for the tab (to be reused)
    strName=Central.Res.getString(R.string.tab_Books);
    intent = new Intent().setClass(this, BookList.class);
    spec = mTabHost.newTabSpec(strName).setIndicator("",
                      res.getDrawable(R.drawable.ic_menu_database))
                  .setContent(intent);
    mTabHost.addTab(spec);


    // Create an Intent to launch an Activity for the tab (to be reused)
    strName=Central.Res.getString(R.string.tab_Questions);
    intent = new Intent().setClass(this, QuestionsList.class);
    intent.putExtra("NoteType",  UserDataSQLHelper.NOTE_TYPE_QUEST );
    spec = mTabHost.newTabSpec(strName).setIndicator("",
                      res.getDrawable(R.drawable.ic_menu_dialog))
                  .setContent(intent);
    mTabHost.addTab(spec);

My problem is that I have the same window title for every tab. This window title is set to application name.

I need to be able to change this title to the name of the tab.

I am trying to do it by calling setTitle("MyTitle"); in onCreate() of corresponding activity and by overriding TabActivity onChildTitleChanged:

        public void onChildTitleChanged(Activity childActivity, CharSequence title)
        {
             View tabView = mTabHost.getCurrentTabView();
             int idTitle = android.R.id.title;
             TextView tvTitle = (TextView) tabView.findViewById(idTitle);
             tvTitle.setText(title);
        }

But the result I get isn't what I need - it sets text to tab under the tab icon.

Could you please help me?


try with following code,

tabHost.setOnTabChangedListener(new OnTabChangeListener() {         
            @Override
            public void onTabChanged(String tabId) {                
                Log.i("Tab id", ""+tabId);
                setTitle(tabId);
            }
        });     


setIndicator (CharSequence label, Drawable icon).
Use this function and the label will be shown as a title.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜