2015-04-24 5 views
0

Обратите внимание, что я читаю html-файл с res> raw> index.html (иерархия папок), который также имеет style.css файл.Добавить CSS в WebView

Но как я добавить файл CSS ... Я не с помощью "файла: /// android_asset" путь

Вот код:

package com.veereshc.veer.vturesults; 

import android.content.Context; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 


public class MainActivity extends ActionBarActivity { 

WebView webView; 
Context context; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    webView = new WebView(this); 
    setContentView(webView); 
    context = this; 
    WebViewClient client = new WebViewClient(); 
    webView.setWebViewClient(client); 



     try { 
      InputStream stream = this.getAssets().open("index.html"); 
      int streamSize = stream.available(); 
      byte[] buffer = new byte[streamSize]; 
      stream.read(buffer); 
      stream.close(); 
      String html = new String(buffer); 
      webView.loadData(readTextFromResource(R.raw.index), "text/html", "utf-8"); 

    } 
    catch (IOException e){ 
     e.printStackTrace(); 
    } 



} 

private String readTextFromResource(int resourceID) 
{ 
    InputStream raw = getResources().openRawResource(resourceID); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    int i; 
    try 
    { 
     i = raw.read(); 
     while (i != -1) 
     { 
      stream.write(i); 
      i = raw.read(); 
     } 
     raw.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    return stream.toString(); 
} 
@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    if (webView.canGoBack()) { 
     webView.goBack(); 
     return; 
    } else { 
     super.onBackPressed(); 
    } 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 




@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

+0

получил это работает, добавив внутренний стиль CSS – Bazzi

ответ

0

вы можете добавить встроенные стили в файл index.html:

<style> 
    body { 
     background-color: green; 
    } 

    h1 { 
     color: red; 
     margin-left: 40px; 
    } 
    ... 
</style> 

или вы можете создать styles.css, включите их:

<link rel="stylesheet" href="styles.css"> 

, а затем использовать WebView#loadDataWithBaseURL:

webView.loadDataWithBaseURL("file:///android_res/raw/", loadData(readTextFromResource(R.raw.index), "text/html", "utf-8", ""); 
Смежные вопросы