簡體   English   中英

如何以編程方式/動態方式更改TabHost的標簽

[英]How to Change Label of TabHost Programmatically/Dynamically

我有一個由創建的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”。 我找不到任何例子。 感謝您的關注。

編輯:我想從“Mənzərələr” =>“ secondTabitem”更改第二個Tabitem的標簽

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

嘗試這個:

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

希望它會有所幫助。

bi的代碼只能更改第一個標簽的標題。 我認為這是完成任務的更直接的方法:

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

其中position是標簽的位置,而yourTitle是您希望為標簽設置的標題。 如果要更改當前選項卡的文本,則可以用getCurrentTabView()代替getTabWidget().getChildTabViewAt(position) ,而不必用getCurrentTab代替position getTabWidget().getChildTabViewAt(position)

撰寫此內容的人應該已經定義了setTabText(int position, String text)方法,否則誰會知道他們有一個由android.R.id.title標識的文本視圖? 或者,如果他們已經擁有,請啟發我。

試試這個代碼

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM