【问题标题】:WebView setBackgroundColor not work in AndroidWebView setBackgroundColor 在 Android 中不起作用
【发布时间】:2016-07-16 08:57:30
【问题描述】:

我已经尝试了我发现的所有建议,但没有人适合我。 此代码不会影响 Android 中 webview 上的背景颜色:

myWebView = (WebView) findViewById(R.id.webview1);

myWebView.setWebChromeClient(new WebChromeClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings();
myWebView.setBackgroundColor(0);

我怎样才能让作品变得如此简单?

【问题讨论】:

  • 我试过黑色、白色和透明。现在我不尝试透明。
  • 在下面查看我的答案。
  • 对我不起作用 :-(
  • 你不能为 webview 设置背景颜色,我的意思是这不起作用

标签: android android-layout webview background-color setbackground


【解决方案1】:

改一下就行了

myWebView.setBackgroundColor(0);

到这里

myWebView.setBackgroundColor(Color.BLUE);

因为它工作正常。

查看我的完整代码..

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.softeng.abcd.Main2Activity"
    tools:showIn="@layout/activity_main2"
   >

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webview"></WebView>

</RelativeLayout>

java 代码。

 WebView webview = (WebView)findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.getSettings();
        webview.setBackgroundColor(Color.BLUE);

输出:

【讨论】:

  • @Luca Panteghini 查看我的更新答案。如果它不起作用,请尝试clean该项目。
  • 蓝色、黄色或绿色可以使用,白色或透明则不行。奇怪的事情!!!在相同情况下显示为灰色而不是白色。
  • @LucaPanteghini 在white 的情况下,您已将parent 背景设置为其他Color,在transaparent 的情况下,它也可以工作,但人类可以'看不到transparent 颜色,因此无法显示我们。否则它也可以工作。
  • @LucaPanteghini 很高兴为您提供帮助。
  • 这对我来说也只适用于某些颜色。红、绿、蓝、黄,都很好。白色,灰白色,不工作。关于如何让它工作的任何想法?
猜你喜欢
  • 2022-01-20
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 2018-09-13
  • 2012-05-17
相关资源
最近更新 更多