在TabHost中的Webview = 启动为黑屏状态

3
我试图在一个包含4个标签的tabhost中使用webview,所有标签都链接到同一个webview。
这很好,但有一个问题: 刚开始时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: Could not create tab content because could not find view with 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)); 

将其从 FrameLayout 中移除。将其作为 LinearLayout 的子项添加。 - Sherif elKhatib
这个还能让我通过我的 TabHost 进行控制吗? - David K
好的,那我今晚尝试一下 :) 如果可以的话,我会在这里发帖,并且你可以将你的解决方案作为答案发布以获取赏金。 - David K
将WebView从FrameLayout移动到LinearLayout中似乎会导致应用程序在启动时强制关闭。 - David K
2个回答

2

0
我曾经遇到过类似的问题。按照建议,我将tabHost.getCurrentView().setVisibility(View.VISIBLE);添加到代码中,但WebView仍然是空白的。在进行了更多搜索之后,this answer帮助了我。原来设置android:layout_height="wrap_content"对于WebView非常重要。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接