我试图在一个tabhost中使用一个webview,这个tabhost有4个tab-都链接到同一个webview。
这很好,除了一个问题:
启动时网络视图为黑色。
点击标签2、3或4使其“活跃起来”。
我的快速修复方法是使用setcurrenttab(1),然后返回到0,但这看起来很难看,所以我想我可能会要求一个解决方案,因为我在网上找不到任何东西。
怎么能解决这个问题?下面是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<android.webkit.WebView android:layout_width="fill_parent" android:id="@+id/webview" android:layout_height="fill_parent" android:scrollbars="none"/>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
</LinearLayout>
</TabHost>
更新:
将webview置于framelayout之外会导致应用程序在启动时崩溃,并出现以下错误:
java.lang.RuntimeException:无法创建选项卡内容,因为找不到ID为2131099648的视图
当我在oncreate方法中初始化tabhost时会发生这种情况,如下所示:
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Tab1", getResources().getDrawable(R.drawable.ligenu)).setContent(R.id.webview));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Tab2", getResources().getDrawable(R.drawable.mad)).setContent(R.id.webview));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Tab3", getResources().getDrawable(R.drawable.godpris)).setContent(R.id.webview));
mTabHost.addTab(mTabHost.newTabSpec("tab_test4").setIndicator("Tab4", getResources().getDrawable(R.drawable.om)).setContent(R.id.webview));
最佳答案
突破!
我在另一篇博文中找到了自己问题的答案,这篇博文让我在过去没有偶然发现:
Why is my TabHost's FrameLayout's only child loaded with visibility = View.GONE?
简单设置:
tabHost.getCurrentView().setVisibility(View.VISIBLE);
解决问题!