Android WebView 背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10919223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:28:11  来源:igfitidea点击:

Android WebView background color

androidwebviewbackground-color

提问by Vervatovskis

I am adding to my layout a WebView to display justified text. I want to set the background of the WebView to be transparent to appear like a textView. Here's what I did:

我在我的布局中添加了一个 WebView 来显示对齐的文本。我想将 WebView 的背景设置为透明以显示为 textView。这是我所做的:

WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.setBackgroundColor(0x00000000);

It works on the emulator, but when I run the application on my device it doesn't work: what I get is a white background.

它适用于模拟器,但是当我在我的设备上运行应用程序时它不起作用:我得到的是白色背景。

 String textTitleStyling = "<head><style>* {margin:0;padding:0;font-size:20; text-align:justify; color:#FFFFFF;}</style></head>";
 String titleWithStyle = textTitleStyling + "<body><h1>" + movie.synopsis + "</h1></body>";
 synopsis.loadData(textTitleStyling + movie.synopsis, "text/html", "utf-8");
 synopsis = (WebView) findViewById(R.id.synopsis);
 synopsis.getSettings();
 synopsis.setBackgroundColor(0);

回答by Rookie

Try using synopsis.getSettings();

尝试使用 synopsis.getSettings();

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

回答by duggu

try below code hope use full for you:-

试试下面的代码希望对你有用:-

webview.setBackgroundColor(Color.parseColor("#919191"));

grey code : #919191

格雷码: #919191

回答by Oubaida AlQuraan

You must put this in the XML code :

你必须把它放在 XML 代码中:

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

for your web view like this for example :

对于像这样的网络视图,例如:

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

and after this you must go to Java code and write this before loadUrl :

在此之后,您必须转到 Java 代码并在 loadUrl 之前编写此代码:

yourWebView.setBackgroundColor(Color.TRANSPARENT);

回答by user1256477

What I do is

我做的是

 synopsis.setBackgroundColor(0);

Hope it helps!

希望能帮助到你!

回答by Timothy

Did you load the css in ur webview?

你在你的 webview 中加载了 css 吗?

Something like:

就像是:

synopsis.loadData(textTileStyling, "text/html", "UTF-8");

or

或者

synopsis.loadDataWithBaseURL("", textTileStyling, "text/html", "UTF-8", "");

回答by SGal

Your html code sets everything to white

您的 html 代码将所有内容设置为白色

Replace:

代替:

    String textTitleStyling = "<head><style>* {margin:0;padding:0;font-size:20; " + 
    "text-align:justify; color:#FFFFFF;}</style></head>"; 

    String titleWithStyle = textTitleStyling + "<body><h1>" + movie.synopsis +
    "</h1></body>";

    synopsis.loadData(textTitleStyling + movie.synopsis, "text/html", "utf-8"); 
    synopsis = (WebView) findViewById(R.id.synopsis); 
    synopsis.getSettings(); 
    synopsis.setBackgroundColor(0);

With:

和:

This excludes color from header style and applies the rest of the style only to body element

这从标题样式中排除颜色并将样式的其余部分仅应用于正文元素

    String textTitleStyling = "<head><style>body{margin:0;padding:0;font-size:20; " + 
    "text-align:justify;}</style></head>"; 

    String titleWithStyle = textTitleStyling + "<body><h1>" + movie.synopsis +
    "</h1></body>";

    synopsis.loadData(titleWithStyle, "text/html", "utf-8"); 
    synopsis = (WebView) findViewById(R.id.synopsis); 
    synopsis.getSettings(); 
    synopsis.setBackgroundColor(0);

EDIT: fixed html

编辑:固定 html