Webview in TabHost = Starting as black screen

血红的双手。 提交于 2019-12-12 14:48:08

问题


I'm trying to use a webview inside a tabhost that has 4 tabs - all linked to the same webview.

This is great except for one problem: At start up the webview is black. Clicking tab 2,3 or 4 makes it "come alive".

My quick fix was to use setCurrentTab(1) and then back to 0, but this looks ugly, so I figured I might as well ask for a solution as I cannot find anything online.

How can this be fixed? Below is my 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>

Update: Putting the webview outside of the framelayout causes the app to crash at startup with following error: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131099648

This happens when I in the onCreate method initialize my tabhost like this:

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

回答1:


Break through!

I found the answer to my own question in another SO post that I didn't stumble upon in the past: Why is my TabHost's FrameLayout's only child loaded with visibility = View.GONE?

Simply setting:

tabHost.getCurrentView().setVisibility(View.VISIBLE);

That fix the issue!




回答2:


I was having similar problem. As suggested, I put tabHost.getCurrentView().setVisibility(View.VISIBLE); to the code, the webview still come out blank. After a few more searches, this answer saved me. It turns out that it's important to set android:layout_height="wrap_content" to the webview.



来源:https://stackoverflow.com/questions/7680714/webview-in-tabhost-starting-as-black-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!