网站标题和图标在移动浏览器中显示,但未在安卓 Webview 中显示

huangapple go评论91阅读模式
英文:

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

网站标题和图标在移动浏览器中显示,但未在安卓 Webview 中显示

In Webview

网站标题和图标在移动浏览器中显示,但未在安卓 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(&quot;http://www.chaderhut24.com&quot;);
    //webView.loadData(&quot;&lt;html&gt;&lt;body&gt;Hello, world!&lt;/body&gt;&lt;/html&gt;&quot;, &quot;text/html&quot;, &quot;UTF-8&quot;);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) &amp;&amp; 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(&quot;chaderhut24.com&quot;)) 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" />

&lt;application
    android:allowBackup=&quot;true&quot;
    android:icon=&quot;@mipmap/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:usesCleartextTraffic=&quot;true&quot;
    android:theme=&quot;@style/AppTheme&quot;
    tools:targetApi=&quot;m&quot;&gt;

    &lt;activity
        android:name=&quot;chaderhut.feni.shopping.dagonbhuiyan.MainActivity&quot;
        android:label=&quot;@string/app_name&quot; &gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;

答案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

huangapple
  • 本文由 发表于 2020年10月26日 23:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64539742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定