英文:
Website Title and Icon Showing in mobile browser but not shown in android webview
问题
以下是翻译好的内容:
我为我的 WordPress 网站制作了一个应用程序。在移动浏览器(如 Chrome、Firefox 等)中打开时,一切正常,但在 Android WebView 中无法正常工作。在 Android WebView 中未显示标题、图标。解决方案是什么呢?
Chrome 视图
![](https://i.stack.imgur.com/BRKU3.png)
在 WebView 中
![](https://i.stack.imgur.com/gwHpo.png)
**Mainactivity.java 代码**
public class MainActivity extends Activity {
private WebView webView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("http://www.chaderhut24.com");
//webView.loadData("<html><body>Hello, world!</body></html>", "text/html", "UTF-8");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
**webviewclientimpl.java**
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.contains("chaderhut24.com")) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}
**AndroidManifest.xml**
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme"
tools:targetApi="m">
<activity
android:name="chaderhut.feni.shopping.dagonbhuiyan.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意:由于您要求只提供翻译内容,因此已省略代码中的注释部分。
英文:
I Make an apps for my wordpress website. It is fine when I open it in mobile browser like chrome, firefox etc, But it does not work Properly in android webview. It did not show title, icon in android webview. What is the solution right now.......
chrome view
In Webview
Mainactivity.java Code
public class MainActivity extends Activity {
private WebView webView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
webView.setWebViewClient(webViewClient);
webView.loadUrl("http://www.chaderhut24.com");
//webView.loadData("<html><body>Hello, world!</body></html>", "text/html", "UTF-8");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
webviewclientimpl.java
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if(url.contains("chaderhut24.com")) return false;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
return true;
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme"
tools:targetApi="m">
<activity
android:name="chaderhut.feni.shopping.dagonbhuiyan.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
答案1
得分: 1
我认为这是因为默认情况下,Android WebView 只会加载网页 body 标签之间的内容。
所以,要加载页面的标题和图标,您需要使用自定义的 WebView。
WebView 类有一些公共方法,比如 getTitle() 和 getFavicon(),可能会对您有帮助。
您可以参考官方的 文档。
英文:
I think this is due to the fact that an Android WebView only loads the contents between the body tag of the webpage by default.
So to load the title and the favicon of the page you will have to use a custom WebView.
A good example code is answered in this question
The WebView class has some public methods like getTitle() and getFavicon() which may help you.
You can refer to the official docs
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论