【问题标题】:Android: Tabwidget (three tabs) with one webview (not all seperate webviews)Android:带有一个 webview 的 Tabwidget(三个选项卡)(并非所有单独的 webviews)
【发布时间】:2012-06-18 18:40:20
【问题描述】:

我要做的是让我的 TabWidget 中的选项卡指向一个 webview,而不是所有三个都有一个单独的 webview。

到目前为止,我所做的基本上是采用 Tab-Layout-tutorial 并在不同的选项卡中创建 web 视图。这很好用。

问题在于,这显然是同时打开了三个网站。这不是我想要完成的。我想要的是所有三个选项卡都链接到一个 webview。

我一直在寻找答案,但很遗憾找不到。我确实发现有人在 SO here 上做了完全相同的事情,但不幸的是我自己不太明白如何做。

目前我的应用程序与教程中的制作方式非常相似,当然除了在选项卡中具有三个单独的 webviews(不是 textviews),就像这样(完全一样,除了名称,时间3):


public class SongsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.webview);
          WebView webView = (WebView) findViewById(R.id.myWebView);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.setWebViewClient(new WebViewClient());
          webView.loadUrl("http://www.google.com");
         }
}

class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        boolean result = false;
        /* ... */
        // Return false to proceed loading page, true to interrupt loading
        return result;
    }
}

然后我有 ma​​in.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"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

和一个 webview.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<WebView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/myWebView"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />

我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hellotabwidget.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HelloTabWidgetActivity"
            android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity android:name=".SongsActivity"  android:label="@string/app_name"></activity>
            <activity android:name=".AlbumsActivity"  android:label="@string/app_name"></activity> 
            <activity android:name=".ArtistsActivity"  android:label="@string/app_name"></activity> 
    </application>

</manifest>

【问题讨论】:

  • @Emile.. 如果想使用单个 webview 从选项卡加载不同/或相同的网站,然后使用 Webview 创建三个选项卡和一个活动并获取 webview 的实例并在 OnClick 事件中传递 url方法,试试这个..还有什么问题让我知道我会发布示例代码
  • 谢谢你!我希望明天会继续努力,否则会在 2 天后回来报告。再次感谢您

标签: android tabs webview android-tabhost


【解决方案1】:

我不太确定,但请尝试使用单例:

    setContentView(R.layout.webview);
    WebView webView = (WebView) findViewById(R.id.myWebView);
    webView.getSettings().setJavaScriptEnabled(true);
    // webView.setWebViewClient(new WebViewClient());
    webView.setWebViewClient(MyWebViewClient.getInstance());
    webView.loadUrl("http://www.google.com");

    public class MyWebViewClient extends WebViewClient {

    private static MyWebViewClient uniqInstance;

private MyWebViewClient() {
super();
}

public static synchronized MyWebViewClient getInstance() {
if (uniqInstance == null) {
uniqInstance = new MyWebViewClient();
}
return uniqInstance;
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2014-01-27
    相关资源
    最近更新 更多