如何更改网页视图中的背景

How to change background in webview

我为 activity 创建了两个主题; Bright/Dark。但是当我触发主题更改时,webview 中加载的网页颜色没有改变。

我试过了myWebView.setBackgroundColor(Color.TRANSPARENT); 但这没有任何区别。那么,我该怎么做呢?

这是我的布局,请看一下;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="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:orientation="vertical">   

    <LinearLayout
        xmlns:night="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@color/ColorPrimary"
        night:night_background="@color/colorPrimary_n"
        tools:ignore="MissingPrefix">

        <ProgressBar
            android:id="@+id/progressBar3"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="3dp"
            android:max="100"
            android:progressDrawable="@drawable/greenprogress"
            />

        <android.support.v4.widget.SwipeRefreshLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <WebView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/webViewTop"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                night:night_background="@color/colorPrimary_n"
                android:layout_below="@id/progressBar3"/>

        </android.support.v4.widget.SwipeRefreshLayout>

    </LinearLayout>
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

您可以像下面这样以编程方式实现它

webView.getSettings();
webView.setBackgroundColor(Color.TRANSPARENT);

我认为你也应该使用图层类型

mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, 空);

祝你好运:)

您可以在 java

试试这个
webView.setBackgroundColor(Color.TRANSPARENT);
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);

试试这个

WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.getSettings();
synopsis.setBackgroundColor(Color.TRANSPARENT);

将此代码放入 xml:

android:background="@android:color/transparent"


<WebView
    android:id="@+id/MyWebView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:scrollbars="none" />

在此之后,您只需转到 Java 代码并在 loadUrl 之前写入:

yourWebView.setBackgroundColor(Color.TRANSPARENT);

好吧,在四处搜索之后,我发现,要在加载后更改网页的颜色,CSS 需要自定义,webview 中的任何设置都不能使网页透明(至少在我的情况下) ).