【问题标题】:Android: How to dynamically change inflated tab content?Android:如何动态更改膨胀的标签内容?
【发布时间】:2011-06-22 20:38:49
【问题描述】:

我在设置标签上的内容时遇到了问题,该标签是从 XML 文件扩展而来的。

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

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

这很好用...我遇到的问题是稍后更改该选项卡上的内容。有没有办法在不使用全新布局重新填充所有选项卡的情况下完成此操作?

我正在尝试以下操作,但没有任何反应:

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

【问题讨论】:

    标签: android android-tabhost layout-inflater


    【解决方案1】:

    您可以保留对 layout 变量的引用(可能在地图或其他东西中),然后稍后以编程方式对其进行修改,如下所示:

    tabMap.get(tabId).findViewById(R.id.testText).setText("The text is changed now!");
    

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

    【讨论】:

      【解决方案2】:

      试着去做

      t.invalidate();
      

      这应该强制它重绘

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-11
        • 2017-10-21
        • 2011-08-29
        • 2023-03-14
        • 1970-01-01
        • 2011-04-09
        • 1970-01-01
        相关资源
        最近更新 更多