Android WebView 背景颜色

66

我正在我的布局中添加一个 WebView 来显示对齐文本。我希望将 WebView 的背景设置为透明,以便看起来像一个 textView。这是我所做的:


我正在我的布局中添加一个 WebView 来显示对齐文本。我希望将 WebView 的背景设置为透明,以便看起来像一个 textView。这是我所做的:
WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.setBackgroundColor(0x00000000);

在模拟器上可以运行,但是当我在设备上运行应用时,它不起作用:我得到的只是一个白色背景。

 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);
9个回答

112

尝试像这样设置背景:

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

2
这种方法仅在直接指定颜色时才有效,例如:synopsis.setBackgroundColor(Color.Black);已在三星Tab 4 7英寸安卓4.4.2上测试通过。 - Choletski
36
“getSettings()” 的意义是什么?你没有使用它。 - SpaceMonkey
2
我同意,“geSettings()”不是必要的。不过这个解决方案还是很好用的 :) - EduardoFernandes

33

尝试以下代码,希望对您有所帮助:

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

灰色编码:#919191


20

在XML代码中,您必须放置以下内容:

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

例如,对于你的 Web 视图:

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

接着你需要进入Java代码,在loadUrl之前添加以下内容:

yourWebView.setBackgroundColor(Color.TRANSPARENT);

17
我不需要在XML中添加android:background="@android:color/transparent",只需在代码中使用setBackgroundColor(Color.TRANSPARENT)即可。(仅更改XML对我来说行不通) - nibarius
1
如果使用Xamarin,只需使用webview.SetBackgroundColor(Android.Graphics.Color.Transparent);即可。 - Vahid Amiri

3
这是我唯一找到的方法,可以让它在打开深色模式时不加载初始的白色背景:
webView.setBackgroundColor(Color.TRANSPARENT);
webView.setVisibility(View.VISIBLE);


<WebView
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible"
   />

2

What I do is

 synopsis.setBackgroundColor(0);

希望对你有帮助!

也许你应该把整个代码(包括HTML)写出来,因为我担心错误就在那里。 - user1256477
这是代码 { 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);} - Vervatovskis

2
你是否在网页视图中加载了CSS样式表?
类似这样的内容:
synopsis.loadData(textTileStyling, "text/html", "UTF-8");

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

或synopsis.loadDataWithBaseURL(“”,textTileStyling,“text/html”,“UTF-8”,“”); - Timothy
谢谢您的回答,我已经编辑了我的帖子,您会发现我是如何加载数据的。 - Vervatovskis

1

你的html代码将所有东西都设为白色

替换为:

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

使用:

这将从标题样式中排除颜色,并仅将其余样式应用于主体元素

    String textTitleStyling = "<head><style&#62body{margin:0;padding:0;font-size:20;text-align:justify;}&#60/style&#62&#60/head&#62";
String titleWithStyle = textTitleStyling + "&#60body&#62&#60h1&#62" + movie.synopsis + "&#60/h1&#62&#60/body&#62";
synopsis.loadData(titleWithStyle, "text/html", "utf-8"); synopsis = (WebView) findViewById(R.id.synopsis); synopsis.getSettings(); synopsis.setBackgroundColor(0);
</pre>
<p>编辑:修正了HTML格式</p>


0

你也可以做到 -

webview.setBackgroundColor(getContext().getResources().getColor(android.R.color.transparent));

android.R.color.transparent 是属于Android框架的透明颜色


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接