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);
34
demandé sur PoorBob 2012-06-06 21:35:23

7 réponses

essayez d'utiliser synopsis.getSettings ();

WebView synopsis;
synopsis=(WebView)findViewById(R.id.synopsis);
synopsis.getSettings();
synopsis.setBackgroundColor(Color.TRANSPARENT);
62
répondu Rookie 2016-02-23 18:50:28

essayez ci-dessous code hope utiliser pleinement pour vous: -

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

code gris: #919191

18
répondu duggu 2017-05-11 03:45:07

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);
12
répondu Oubaida AlQuraan 2017-04-13 22:27:35

Ce que je fais est

 synopsis.setBackgroundColor(0);

Espère que cela aide!

2
répondu user1256477 2012-06-06 19:50:51

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", "");
2
répondu Timothy 2012-06-11 04:31:42

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

1
répondu SGal 2013-07-05 14:22:33