如何以编程方式/动态更改TabHost的标签

时间:2012-04-26 08:21:15

标签: android android-tabhost

我有一个由

创建的tabhost
  this.tabHost = getTabHost();

     // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch the first Activity for the tab (to be reused)
    intent = new Intent().setClass(this, FirstGroup.class);

    // Initialize a TabSpec for the first tab and add it to the TabHost
    spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
            getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
                    .setContent(intent);
    tabHost.addTab(spec1);

我想以编程方式更改tabhost的标签:“Regionlar”改为“newMenuTabbar”。我找不到任何例子。谢谢你的关注。

编辑: 我想改变第二个tabitem的标签来自“Mənzərələr”=> “secondTabitem”

intent = new Intent()。setClass(this,FirstGroup.class);

    // Initialize a TabSpec for the first tab and add it to the TabHost
    spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
            getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
                    .setContent(intent);
    tabHost.addTab(spec1);

        // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, SecondActivityGroup.class);
    spec2 = tabHost.newTabSpec("SecondActivityGroup").setIndicator("Mənzərələr",
            getResources().getDrawable(R.drawable.img_gallery_icon)) // Replace null with R.drawable.your_icon to set tab icon
                    .setContent(intent);
    tabHost.addTab(spec2);

3 个答案:

答案 0 :(得分:5)

试试这个:

final TextView label = (TextView) tabHost.getTabWidget().findViewById(android.R.id.title);
label .setText(YOUR NEW LABEL);

希望它会有所帮助。

答案 1 :(得分:1)

b.i的代码只能更改第一个标签的标题。 我认为这是完成任务的更直接方式:

((TextView) mTabHost.getTabWidget().getChildTabViewAt(position)
.findViewById(android.R.id.title))
.setText(yourTitle);

其中position是标签的位置,yourTitle是您要为标签设置的标题。如果您想更改当前标签的文字,那么您只需替换position,而不是按getCurrentTab替换getTabWidget().getChildTabViewAt(position)。 按getCurrentTabView()

无论谁写这个都应该定义一个setTabText(int position, String text)方法,否则谁会知道他们有一个id android.R.id.title的文本视图?或者,如果他们已经有,请赐教。

答案 2 :(得分:0)

试试此代码

public class SlideMainActivity extends TabActivity {
    public static RelativeLayout headerLayout;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main_silde_tab);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.hd_header);
        setTabs();
    }

    private void setTabs() {
        addTab("FirstGroup", R.drawable.tab_home, FirstGroup.class);
        addTab("Regionlar", R.drawable.tab_search, Regionlar.class);

    }

    private void addTab(String labelId, int drawableId, Class<?> c) {
        TabHost tabHost = getTabHost();
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

        View tabIndicator = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

}