Android:如何动态更改膨胀的选项卡内容?
我在设置从 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??");
I am having trouble setting the content on a tab that I've inflated from an XML file.
I add the tab to my TabHost ('tabs') dynamically by doing the following:
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);
This works fine...what I'm having trouble with is changing the content on that tab later on. Is there any way to accomplish this without re-inflating all of the tabs with brand new layouts?
I am trying the following and nothing happens:
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??");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以保留对
layout
变量的引用(可能在地图或其他内容中),然后以编程方式对其进行修改,如下所示:只要您在 UI 线程上执行此操作,更改就会立即反映出来。
You can keep a reference to the
layout
variable (maybe in a map or something) and then programmatically modify it later on like this:As long as you do it on the UI thread the changes should be reflected immediately.
尝试这样做
这应该会强制它重绘
Try to do
This should force it to redraw