Couleur de fond de WebView Android
j'ajoute à ma mise en page un WebView pour afficher du texte justifié. Je veux que L'arrière-plan du WebView soit transparent pour apparaître comme un textView. Voici ce que j'ai fait:
WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.setBackgroundColor(0x00000000);
il fonctionne sur l'émulateur, mais quand j'exécute l'application sur mon appareil il ne fonctionne pas: ce que j'obtiens est un fond blanc.
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);
7 réponses
essayez d'utiliser synopsis.getSettings ();
WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.getSettings();
synopsis.setBackgroundColor(Color.TRANSPARENT);
essayez ci-dessous code hope utiliser pleinement pour vous: -
webview.setBackgroundColor(Color.parseColor("#919191"));
code gris: #919191
vous devez le mettre dans le code XML:
android:background="@android:color/transparent"
pour votre vue web comme ceci par exemple :
<WebView
android:id="@+id/MyWebView"
android:layout_width="fill_parent"
android:layout_height="62dp"
android:background="@android:color/transparent"
android:scrollbars="none" />
et après cela, vous devez aller au code Java et écrire ceci avant loadUrl:
yourWebView.setBackgroundColor(Color.TRANSPARENT);
Ce que je fais est
synopsis.setBackgroundColor(0);
Espère que cela aide!
avez-vous chargé le css dans ur webview?
quelque chose comme:
synopsis.loadData(textTileStyling, "text/html", "UTF-8");
ou
synopsis.loadDataWithBaseURL("", textTileStyling, "text/html", "UTF-8", "");
, Vous pouvez trouver quelques conseils ici: http://code.google.com/p/android/issues/detail?id=14749 et aussi ici: WebView Android style background-color:transparent ignoré sur android 2.2
votre code html met tout en blanc
remplacer:
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);
avec:
ceci exclut la couleur du style de l'en-tête et applique le reste du style seulement à l'élément de corps
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