开发者

Webview in TabHost = Starting as black screen

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 tabh开发者_高级运维ost 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)); 


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜