<LinearLayout
android:id="@+id/TabContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="99"
android:orientation="vertical">
<TabHost
android:id="@+android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/TabLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@+android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@+android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"></FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
基本上它是一个带有TextViews作为内容的TabHost.我正在使用createTabContent()动态构建这些TextViews的选项卡和内容.这很好.但在某些情况下(某些事件由带有TCP套接字的AsyncTask处理),我需要更改其中一些TextView的内容.
我确信我的问题是我对Android SDK缺乏经验的结果,但我找不到迭代TabHost并获取每个TextView的内容的方法.
我所取得的“最接近”的方法是:
TabHost th = (TabHost) findViewById(android.R.id.tabhost);
TabWidget tw = th.getTabWidget();
for (int i = 0; i < tw.getChildCount(); i++) {
LinearLayout lLayout = (LinearLayout) tw.getChildAt(i);
TextView tv = ((TextView) lLayout.getChildAt(i));
tv.append(linea);
}
但这样做是将“linea”附加到选项卡的名称(指示符).我想将它添加到该选项卡的内容,但我找不到一种方法来检索与之关联的TextView.
任何想法将非常感谢!
———编辑———
虽然在下面的评论中我发布了我已经有解决方案,但我发现它无效.代码现在看起来像这样:
for (int i = 0; i < tw.getChildCount(); i++) {
LinearLayout LLayout = (LinearLayout) tw.getChildAt(i);
TextView currenttab = (TextView) LLayout.getChildAt(0);
if (tab.equals(currenttab.getText())) {
th.setCurrentTab(i);
TextView tabContent = (TextView) th.getTabContentView().getChildAt(0);
tabContent.append(linea);
return true;
}
}
问题是getTabContentView()仅引用当时活动的Tab,所以如果我想在未选择的选项卡的TextView中添加一些文本,我必须先使用.setCurrentTab(i)选择它,这意味着我要改变活动标签……
我认为这比我想的要容易得多……对此有何帮助?
谢谢!
———编辑———
那时我意识到无法访问非活动选项卡的内容.至少不那样.据说您无法访问非活动选项卡的视图,因为它没有关联的视图,至少不是公共视图.
我试图声明一个Map,其中第一个String是选项卡的名称,第二个是与其内容关联的TextView,并在创建选项卡时分配它们.结果是我只能引用活动选项卡的TextView,其他引发了NullPointer异常.
所以我把问题集中在一起.现在,如果我必须将一些文本添加到非活动选项卡中,我将该文本添加到哈希,我声明了一个OnTabchangelistener,每次我更改选项卡时,我都会将待处理内容转储到其中.
这不是最出色的解决方案,但它是我工作的唯一一个……所以如果有人有更好的解决方案,我将不胜感激.
解决方法
yourTabHost.getTabWidget().getChildTabViewAt(position);
此代码返回任何选项卡的视图,甚至是非活动选项卡.