如何更改 TabActivity 中选项卡的标题

发布于 2024-12-06 18:00:39 字数 1958 浏览 2 评论 0原文

我有 TabActivity 底部有选项卡,它由几个不同的活动组成,如下所示:

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);

我的问题是每个选项卡都有相同的窗口标题。该窗口标题设置为应用程序名称。

我需要能够将此标题更改为选项卡的名称。

我试图通过调用 setTitle("MyTitle"); 来做到这一点在相应活动的 onCreate() 中并通过重写 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);
        }

但我得到的结果不是我需要的 - 它将文本设置为选项卡图标下的选项卡。

请你帮助我好吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女的英雄梦 2024-12-13 18:00:39

尝试使用以下代码,

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

try with following code,

tabHost.setOnTabChangedListener(new OnTabChangeListener() {         
            @Override
            public void onTabChanged(String tabId) {                
                Log.i("Tab id", ""+tabId);
                setTitle(tabId);
            }
        });     
栀梦 2024-12-13 18:00:39

setIndicator(CharSequence 标签,Drawable 图标)
使用此功能,标签将显示为标题。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文