2017-03-04 46 views
0

我试图将自定义css添加到android应用程序中的webview内容,但它没有在webview中显示任何效果可能是它没有成功添加。如何在wbeview中注入自定义css

我的自定义字体的CSS:

/* normal */ 
@font-face { 
    font-family: 'PT_Sans-Web'; 
    font-style: normal; 
    font-weight: 400; 
    src: local('PT_Sans Web Regular'), local('PT_Sans-Web-Regular'),url(file:///android_asset/fonts/PT_Sans-Web-Regular.ttf); 
} 

/* bold */ 
@font-face { 
    font-family: 'PT_Sans-Web'; 
    font-style: normal; 
    font-weight: 700; 
    src: local('PT_Sans Web Bold'), local('PT_Sans-Web-Bold'), url(file:///android_asset/fonts/PT_Sans-Web-Bold.ttf); 
} 

的style.css

body { 
font-family: PT_Sans-Web; 
color: #4DB2EC; 
font-size: 15pt; 
width: 100%; 
background: #fff; 
} 

strong { 
font-weight: bold; 
} 

embed,object,img{display: block; max-width:100%; height: auto;} 
.frame {width: 100%} 
#templates{display:none} 
.subs-loading {width: 90%; margin: 2em auto; font-size: 1.5em; } 
#subscriptions { 
position: relative; 
} 

#container { 
padding: 16px; 
} 

p { 
font-family: PT_Sans-Web; 
color: #4DB2EC; 
font-size: 7pt; 
width: 100%; 
background: #fff; 
line-height:11.38pt 
} 

h1 { 
font-size: 2em; 
line-height: 1.2em; 
font-weight: bold; 
color: #222; 
margin: .1em 0 .2em; 
} 

加载代码的WebView:

mWebView.loadDataWithBaseURL(
      "file:///android_asset/", 
      content, 
      "text/html", 
      "utf-8", 
      null); 
string content = "<!DOCTYPE html><html><head><meta charset='UTF-8' />" 
      + "<meta name='viewport' content='width=device-width, initial-scale=1'>" 
      + "<link rel='stylesheet' type='text/css' href='pt_sans.css'>" 
      + "<link rel='stylesheet' type='text/css' href='webview.css'>" 
      + "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">" 
      + "</head><body>" 
      + mPostContent 
      + "</body></html>"; 

我使用的WebView与webChromeclient。但是这在webview中没有任何效果。我做错了吗?在webview中添加css是更好的方法。

在打开的WebView我得到这个错误日志:

E/libEGL: validate_display:99 error 3008 (EGL_BAD_DISPLAY) 

此错误给出了测试设备的Nexus 5X和其他设备工作正常。

回答

0

css文件放入assets文件夹中,然后在html中对其进行寻址。更新您的content变量是这样的:

string content = "<!DOCTYPE html><html><head><meta charset='UTF-8' />" 
     + "<link rel=\"stylesheet\" href=\"file:///android_asset/style.css\">" //<-- Add this line 
     + //... 
+0

它在一个工作以及与Android 6测试设备,但给这个错误 “E/libEGL:validate_display:99错误3008(EGL_BAD_DISPLAY)” 在Nexus 5X保护与Android版本7 –

+0

我不知道错误,但它显然与OpenGL有关,并且不在您的手中。我认为你应该忽略它。 – Mousa