Android WebView背景色

Android WebView背景色,android,webview,background-color,Android,Webview,Background Color,我正在向布局中添加一个WebView以显示对齐的文本。我想将WebView的背景设置为透明,使其看起来像textView。以下是我所做的: WebView synopsis; synopsis=(WebView)findViewById(R.id.synopsis); synopsis.setBackgroundColor(0x00000000); 它在模拟器上工作,但当我在我的设备上运行应用程序时,它不工作:我得到的是白色背景 String textTitleStyling = "<

我正在向布局中添加一个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);
String textTitleString=“*{margin:0;padding:0;字体大小:20;文本对齐:对齐;颜色:#FFFFFF;}”;
字符串titleWithStyle=textTitleString+“”+movie.sympsis+“”;
加载数据(textTitleStyling+movie.synosis,“text/html”,“utf-8”);
大纲=(网络视图)findViewById(R.id.synopsis);
getSettings();
背景色(0);

尝试使用synopsis.getSettings()

我所做的是

 synopsis.setBackgroundColor(0);

希望有帮助

您是否在您的Web视图中加载了css

比如:

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


您可以在这里找到一些提示:也可以在这里找到:

请尝试以下代码,希望为您完全使用:-

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

灰色代码:
919191

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

替换:

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); 字符串textTitleString=“<head><style>*{边距:0;填充:0;字体大小:20;” “文本对齐:对齐;颜色:#FFFFFF;}</样式></头部>”; 字符串titleWithStyle=textTitleStyling+“<body><h1>”+电影简介+ “</h1></body>”; 加载数据(textTitleStyling+movie.synosis,“text/html”,“utf-8”); 大纲=(网络视图)findViewById(R.id.synopsis); getSettings(); 背景色(0); 与:

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); 这将从标题样式中排除颜色,并仅将样式的其余部分应用于主体元素

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); 字符串textTitleString=“<head><style>body{边距:0;填充:0;字体大小:20;” “文本对齐:justify;}</style></head>”; 字符串titleWithStyle=textTitleStyling+“<body><h1>”+电影简介+ “</h1></body>”; 概要.加载数据(标题为“文本/html”、“utf-8”); 大纲=(网络视图)findViewById(R.id.synopsis); getSettings(); 背景色(0);
编辑:修复html

您必须将其放入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" />
你也可以做到-

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

这里的
android.R.color.transparent
是属于android fragmework的透明颜色。

这是我唯一能让它工作并且不首先加载初始白色背景的方法,如果我打开了暗模式:

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"
   />
webView.setBackgroundColor(Color.TRANSPARENT);
设置可见性(View.VISIBLE);

也许你应该写完整的代码(包括html),因为我担心这是个错误。这是代码{String textitleString=“*{margin:0;padding:0;font size:20;text align:justify;color:#FFFFFF;}”;String titleWithStyle=textitleString+”“+movie.synosis+”;synosis.loadData(textTitleString+movie.Synosis,“text/html”,“utf-8”);Synosis=(WebView)findViewById(R.id.Synosis);Synosis.getSettings();Synosis.setBackgroundColor(0);}或Synosis.loadDataWithBaseURL(“,textTileString,“text/html”,“utf-8”,”);谢谢你的回答,我编辑了我的帖子,你会发现我是如何加载数据的我不需要在我的XML中添加安卓:background=“@android:color/transparent”,只需在代码中添加setBackgroundColor(color.transparent);如果使用Xamarin,
webview.setBackgroundColor(仅更改XML对我无效)(Android.Graphics.Color.Transparent);
就足够了。只有当您直接指定颜色时,此方法才有效,例如:
synosis.setBackgroundColor(Color.Black);
在三星Tab 4上测试7“Android 4.4.2使用“getSettings()”有什么意义?您没有使用它。我同意,“geSettings()”“没有必要。不过解决方案效果很好:)
webview.setBackgroundColor(getContext().getResources().getColor(android.R.color.transparent));
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"
   />