简体   繁体   English

TabHost中的Webview =以黑屏开始

[英]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. 我正在尝试在具有4个标签的tabhost中使用webview - 所有标签都链接到同一个webview。

This is great except for one problem: At start up the webview is black. 这很好,除了一个问题:在启动时webview是黑色的。 Clicking tab 2,3 or 4 makes it "come alive". 单击选项卡2,3或4使其“活跃起来”。

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. 我的快速修复是使用setCurrentTab(1)然后回到0,但这看起来很难看,所以我想我也可以请求解决方案,因为我在网上找不到任何东西。

How can this be fixed? 怎么解决这个问题? Below is my XML: 下面是我的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 更新:将webview放在framelayout之外导致应用程序在启动时崩溃并出现以下错误:java.lang.RuntimeException:无法创建选项卡内容,因为找不到ID为2131099648的视图

This happens when I in the onCreate method initialize my tabhost like this: 当我在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)); 

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? 我在另一篇SO帖子中找到了我自己的问题的答案,我过去没有偶然发现: 为什么我的TabHost的FrameLayout唯一的孩子装载了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); 按照建议,我把tabHost.getCurrentView()。setVisibility(View.VISIBLE); to the code, the webview still come out blank. 对于代码,webview仍然是空白。 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. 事实证明,将android:layout_height =“wrap_content”设置为webview是很重要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM