I have a webview that displays some HTML i am generating at runtime. This html works in chrome and IE, but when I try to load it into the webview, it tells me "Web page not available" I'm using basic HTML elements:
<body bgcolor="01517f">
<table>
<font color="ffffff" size="4">
<p>
<a href="http://www.gim.net/Customized/Uploads/Screenshot532011-33538-PM.jpg">
<img src="http://www.gim.net/Customized/Uploads/Screenshot532011-33538-PM.jpg" width="160" height="240" align="right">
</a>
Message body
</p>
<table width="95%" cellpadding="3" cellspacing="0" >
<tr>
<td>
<b>
<font size="3" color="ffffff">
parent1
</font>
</b>
</td>
<td>
<p align="right">
<font size="2" color="ffffff">
This will be the date string
</font>
</p>
</td>
</tr>
<tr>
<td colspan ="2">
<p align="left">
<font size="3" color="ffffff">
Blah blah
</font>
</p>
</td>
</tr>
</table>
<hr noshade size="1" />
</font>
</table>
</body>
For some reason, if I remove the nested table, it seems to display fine. Is there开发者_如何学编程 something non-standard about how I am formatting my HTML that would make it not work with the android webview?
EDIT: After thorough elimination testing, I have discovered that the offending error is from the "width='95%'" attribute of the second table. If I change it to a set pixel width( ex: width="160px") it works. What I need this width for is forcing those elements in that table to be below the image, while still occupying most of the width of the page, not knowing how large my page is going to be, because I do not know how large the screen I might be working with is.
Should I just figure out the screen size, and manually set the pixel width that way, or is there some simpler way I can force a break between the image aligned to the right, and the table that follows below it?
The problem was the way that the Android webview attempts to access the HTML data when you call loadData
on it. I noticed that when I got the error the "web address" it was trying to access was basically my HTML code as a URL, replacing non standard characters with their http friendly versions (%20 for the space character for example). So I simply replaced the % sign in my "width='95%'" with the code for the % sign ( %25 ) and it works perfectly now. No need for CSS or anything like that
- The
font
-Tags are obsolete - Color and Text size should be set using CSS
- If you want to align of your Text to
be left, you can simply do this
using
text-align
in CSS - The
align
-Attribute is deprecated, too. Use CSS to do that. img
-Tags should always have aalt
-Attribute, unless they are part of the Layout.- Both
width
andheight
-Attributes are deprecated, use CSS to do that. Also, don't use a number without any unit. If you want 140 Pixel the write140px
(Unless you want 0, thats just 0 without any unit). - I'm not sure if this is due to this
being a snipped, but a HTML-Page
starts with an
<html>
-Tag and has a<head>
-Tag before the<body>
-Tag. - A HTML-Page should always have a Doctype-Declaration.
If you're not sure if your HTML is valid, go Validate it.
Also, i don't get what you use the first table for. It does simply nothing. It doesn't even have a TableRow or some TableData in it. It's not valid and it's not good for anything. Cut it out.
精彩评论