android - 使用 tabhost 时 WebView 无法正确填充屏幕

标签 android webview android-tabhost tabwidget

我有一个带有 TabWidget 的 TabHost。但是我的 WebView(在每个选项卡中我都有一个 webview)在 TabWidget 之上。所以我看不到我的 TabWidget。我该如何解决这个问题?

布局文件

    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_x="0sp"
        android:layout_y="0sp" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="60sp" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                  <WebView
                      android:id="@+id/webView2"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_gravity="bottom"/>

                  <WebView
                      android:id="@+id/webView3"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent" 
                      android:layout_gravity="bottom" />

                  <WebView
                      android:id="@+id/webView1"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_gravity="bottom"/>

             </FrameLayout>
        </RelativeLayout>
    </TabHost>
</AbsoluteLayout>

Java 公共(public)类 MainActivity 扩展 Activity {

 /** Called when the activity is first created. */
TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost=(TabHost)findViewById(R.id.tabHost);
        tabHost.setup();

        TabSpec spec1=tabHost.newTabSpec("Vandaag");
        spec1.setContent(R.id.webView1);
        spec1.setIndicator("Vandaag");


        TabSpec spec2=tabHost.newTabSpec("Morgen");
        spec2.setIndicator("Morgen");
        spec2.setContent(R.id.webView2);


        TabSpec spec3=tabHost.newTabSpec("Overmorgen");
        spec3.setContent(R.id.webView3);
        spec3.setIndicator("Overmorgen");
        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
        tabHost.getCurrentView().setVisibility(View.VISIBLE);
        tabHost.setup();

        WebView myWebView1 = (WebView) findViewById(R.id.webView1);
        myWebView1.setWebViewClient(new WebViewClient());
        myWebView1.loadUrl("http://www.example.com");

        WebView myWebView2 = (WebView) findViewById(R.id.webView2);
        myWebView2.setWebViewClient(new WebViewClient());
        myWebView2.loadUrl("http://www.example.com");

        WebView myWebView3 = (WebView) findViewById(R.id.webView3);
        myWebView3.setWebViewClient(new WebViewClient());
        myWebView3.loadUrl("http://www.example.com");

}

最佳答案

用垂直的 LinearLayout 替换你的 RelativeLayout,或者正确地使用你的 RelativeLayout,给子元素设置规则以调整它们的大小/以您想要的方式定位。

此外,我真的不推荐硬编码高度,就像您在WebViews 上那样。

关于android - 使用 tabhost 时 WebView 无法正确填充屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18676270/

相关文章:

android - AlarmManager setExact 与 WakefulBroadcastReceiver 有时不精确

android - ListView 中的 EditText 高度与行高不匹配

android - Webview 电子邮件链接 (mailto)

android - 错误/Web 控制台 : Uncaught Error: Too much time spent in unload handler

android - Twitter - 从 webview 检索 pin 代码。安卓

java - 如何在不推荐使用 tabhost 的情况下实现 Android 选项卡菜单

android - 从 Photoshop 到 Android 的颜色问题

android - SQLiteDatabase.rawQuery 中的字符串编码错误

java - 机器人 : tabhost not rendering tabs

android - FragmentTabHost 底部的 TabWidget