2012-06-06 24 views
33

Estoy agregando a mi diseño un WebView para mostrar texto justificado. Quiero establecer que el fondo de WebView sea transparente para que parezca una vista de texto. Esto es lo que hice:Android WebView color de fondo

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

Funciona en el emulador, pero cuando corro la aplicación en el dispositivo que no funciona: lo que se ve es un fondo blanco.

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

Respuesta

57

Probar usando synopsis.getSettings();

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

gracias por la respuesta, pero no funciona – Vervatovskis

+2

Se trabajó para mí .. – Rookie

+1

este método está funcionando ** ** Sólo si se especifica el color directamente gustaría: 'synopsis.setBackgroundColor (en color.Negro); ' probado en Samsung Tab 4 7" Android 4.4.2 – Choletski

2

Lo que hago es

synopsis.setBackgroundColor(0); 

espero que ayude!

+0

gracias por la respuesta, pero no funciona – Vervatovskis

+0

Tal vez deberías escribir el código completo (incluyendo html) porque me temo que el error está ahí. – user1256477

+0

Este es el código Cadena textTitleStyling = " "; \t \t \t \t \t cadena titleWithStyle = textTitleStyling +"

"+ movie.synopsis +"

"; \t \t \t \t \t synopsis.loadData (textTitleStyling + movie.synopsis , "text/html", "UTF-8"); \t \t \t \t \t sinopsis = (WebView) findViewById (R.id.synopsis); \t \t \t \t \t synopsis.getSettings(); \t \t \t \t \t synopsis.setBackgroundColor (0);} – Vervatovskis

2

¿Ha cargado el CSS en su página web?

Algo así como:

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

o

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

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

+0

Gracias por su respuesta, edité mi publicación, encontrará cómo cargué los datos – Vervatovskis

17

intento por debajo de esperanza código de uso completo para usted: -

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

código Gray: #919191

1

su código HTML pone todo a blanco

Reemplazar:

 

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

Con:

Esto excluye el color de estilo de cabecera y aplica el resto del estilo solo al elemento del cuerpo

 

    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: html fijo

12

Debe poner esto en el código XML:

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

para su vista web como este por ejemplo:

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

y después de esto debe ir al código de Java y escribir esto antes de loadUrl:

yourWebView.setBackgroundColor(Color.TRANSPARENT); 
+9

No necesité poner android: background = "@ android: color/transparent" en mi XML, fue suficiente con hacer setBackgroundColor (Color.TRANSPARENTE); en el código. (Cambiar solamente el XML no funcionó para mí) – nibarius

+1

Si usa Xamarin, 'webview.SetBackgroundColor (Android.Graphics.Color.Transparent);' es suficiente. – VSG24