Android:如何动态更改膨胀的标签内容?

Tom*_*omb 5 android android-tabhost layout-inflater

我在从XML文件中填充的选项卡上设置内容时遇到问题.

我通过执行以下操作动态地将选项卡添加到我的TabHost('tabs'):

        TabSpec passSpec = tabs.newTabSpec("Pass Tab"); 
        passSpec.setIndicator("Passengers", getResources().getDrawable(R.drawable.tab_message));

        passSpec.setContent(new TabHost.TabContentFactory() { 
            public View createTabContent(String tag) { 
                View layout = mInflater.inflate(R.layout.tab_content_passengers, null);                 
                return(layout); 
            } 
        });
        tabs.addTab(passSpec);
Run Code Online (Sandbox Code Playgroud)

这很好用......我遇到的问题是稍后更改该选项卡上的内容.有没有办法实现这一点,而不用全新的布局重新膨胀所有选项卡?

我正在尝试以下内容并且没有任何反应:

    mInflater = LayoutInflater.from(this);
    View layout = mInflater.inflate(R.layout.tab_content_passengers, null);
    TextView t = (TextView) layout.findViewById(R.id.testText);
    t.setText("Hello world??");
Run Code Online (Sandbox Code Playgroud)

Gya*_*uyn 4

您可以保留对变量的引用layout(可能在映射或其他内容中),然后稍后以编程方式修改它,如下所示:

tabMap.get(tabId).findViewById(R.id.testText).setText("The text is changed now!");
Run Code Online (Sandbox Code Playgroud)

只要您在 UI 线程上执行此操作,更改就会立即反映出来。