【问题标题】:Android WebView background colorAndroid WebView 背景颜色
【发布时间】:2012-06-10 18:20:45
【问题描述】:

我正在向我的布局中添加一个 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);

【问题讨论】:

    标签: android webview background-color


    【解决方案1】:

    如果我开启了深色模式,这是我可以让它工作而不首先加载初始白色背景的唯一方法:

    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】:

      你也可以这样做-

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

      这里android.R.color.transparent透明色,属于android fragmework。

      【讨论】:

        【解决方案3】:

        尝试使用 synopsis.getSettings();

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

        【讨论】:

        • 此方法在您直接指定颜色时有效,例如:synopsis.setBackgroundColor(Color.Black); 在 Samsung Tab 4 7" android 4.4.2 上测试
        • 使用“getSettings()”有什么意义?你没有使用它。
        • 我同意,“geSettings()”不是必需的。该解决方案虽然效果很好:)
        【解决方案4】:

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

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

        灰色代码:#919191

        【讨论】:

          【解决方案5】:

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

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

          对于您的网络视图,例如:

          <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);
          

          【讨论】:

          • 我不需要将 android:background="@android:color/transparent" 放在我的 XML 中,只需执行 setBackgroundColor(Color.TRANSPARENT);在代码中。 (仅更改 XML 对我不起作用)
          • 如果使用 Xamarin,webview.SetBackgroundColor (Android.Graphics.Color.Transparent); 就足够了。
          【解决方案6】:

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

          替换:

          String textTitleStyling = "”; 字符串 titleWithStyle = textTitleStyling + "

          " + movie.synopsis + "

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

          与:

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

          String textTitleStyling = "”; 字符串 titleWithStyle = textTitleStyling + "

          " + movie.synopsis + "

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

          编辑:固定 html

          【讨论】:

            【解决方案7】:
            【解决方案8】:

            你是否在你的 webview 中加载了 css?

            类似:

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

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

            【讨论】:

            • 或 synopsis.loadDataWithBaseURL("", textTileStyling, "text/html", "UTF-8", "");
            • 感谢您的回答,我编辑了我的帖子,您会发现我是如何加载数据的
            【解决方案9】:

            我做的是

             synopsis.setBackgroundColor(0);
            

            希望对你有帮助!

            【讨论】:

            • 也许你应该写完整的代码(包括html),因为我担心错误就在那里。
            • 这是代码 { String textTitleStyling = "";字符串 titleWithStyle = textTitleStyling + "

              " + movie.synopsis + "

              "; synopsis.loadData(textTitleStyling + movie.synopsis, "text/html", "utf-8");概要 = (WebView) findViewById(R.id.synopsis);概要.getSettings();概要.setBackgroundColor(0);}
            猜你喜欢
            • 1970-01-01
            • 2017-05-24
            • 2012-07-22
            • 2023-04-03
            • 2019-11-10
            • 1970-01-01
            • 2011-06-27
            • 2015-08-13
            • 2015-07-05
            相关资源
            最近更新 更多