gpt4 book ai didi

Android:如何在Tabview中显示Webview?

转载 作者:太空狗 更新时间:2023-10-29 13:42:22 25 4
gpt4 key购买 nike

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="wrap_content"
android:layout_height="wrap_content" />

Java

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

标签 View 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>

标签 View java

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabview);

Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab


intent = new Intent().setClass(this, WebActivity.class);
spec = tabHost.newTabSpec("webview")
.setIndicator("webview", res.getDrawable(R.drawable.info))
.setContent(intent);
tabHost.addTab(spec);

// add other tabs

tabHost.setCurrentTab(0);
}

这将在全屏模式下启动一个 webView。
是否可以在tabview中显示webview?

最佳答案

您可以在这里找到答案:http://developer.android.com/guide/webapps/webview.html#HandlingNavigation

默认情况下,WebView 会在您每次访问新 URL 时启动浏览器,所以会启动浏览器。

为了避免每次点击某些东西时发生这种情况,您需要将 WebViewClient 添加到您的 WebView:

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

如果您需要在用户单击链接时执行某些特定操作,请实现您自己的 WebViewClient:

public 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;
}
}

并使用它:

myWebView.setWebViewClient(new MyWebViewClient());

关于Android:如何在Tabview中显示Webview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4160199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com