我正在尝试在有 4 个选项卡的 tabhost 中使用 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>
Update:将 webview 放在框架布局之外会导致应用程序在启动时崩溃,并出现以下错误:
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));
突破!
我在另一篇我过去没有偶然发现的帖子中找到了我自己问题的答案:为什么我的 TabHost 的 FrameLayout 的唯一子级加载了visibility = View.GONE? https://stackoverflow.com/questions/5109081/why-is-my-tabhosts-framelayouts-only-child-loaded-with-visibility-view-gone
简单设置:
tabHost.getCurrentView().setVisibility(View.VISIBLE);
这样就解决了问题!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)