使用Android样式定义HTML元素的外观

使用Android样式定义HTML元素的外观,android,html,Android,Html,我正在尝试生成一个表,该表将不同的颜色分配给不同的值,类似于热图 为了实现这一点,我使用java生成HTML,然后将其显示在文本视图中。CSS用于颜色。不幸的是,Android不支持使用Html.fromHtml方法的CSS 下面是使用的代码。是否可以定义一个与CSS执行相同功能的样式?如果没有,最好的方法是什么 谢谢 public String ReturnHeatMapHTML() { // CalculateStartEnd(); String _html =

我正在尝试生成一个表,该表将不同的颜色分配给不同的值,类似于热图

为了实现这一点,我使用java生成HTML,然后将其显示在文本视图中。CSS用于颜色。不幸的是,Android不支持使用Html.fromHtml方法的CSS

下面是使用的代码。是否可以定义一个与CSS执行相同功能的样式?如果没有,最好的方法是什么

谢谢

public String ReturnHeatMapHTML() {

    //
    CalculateStartEnd();

    String _html = "<html>" + System.getProperty("line.separator");

    // HTML Head
    _html += htmlHead();

    // HTML Body open
    _html += "<body>" + System.getProperty("line.separator");

    // HTML Body open
    _html += "<table>" + System.getProperty("line.separator");

    // Title Row
    _html += "<tr><th></th><th>00:00</th><th>01:00</th><th>02:00</th><th>03:00</th><th>04:00</th><th>05:00</th><th>06:00</th><th>07:00</th><th>08:00</th><th>09:00</th><th>10:00</th><th>11:00</th><th>12:00</th><th>13:00</th><th>14:00</th><th>15:00</th><th>16:00</th><th>17:00</th><th>18:00</th><th>19:00</th><th>20:00</th><th>21:00</th><th>22:00</th><th>23:00</th></tr>"
            + System.getProperty("line.separator");

    // Day
    DateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US);
    for (int _days = 0; _days < 7; _days++) {
        _html += "<tr><td>" + dateFormat.format(addDays(_startDate, _days))
                + "</td>";

        Calendar calFrom = Calendar.getInstance();
        calFrom.setTime(addDays(_startDate, _days));
        Date _templateDate = new Date(calFrom.get(Calendar.YEAR) - 1900,
                calFrom.get(Calendar.MONTH),
                calFrom.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        for (int _hours = 0; _hours < 24; _hours++) {
            String val = returnAvailbility(_templateDate);
            _html += "<td class='col_" + val + "'>" + val + "</td>";
            _templateDate = addHours(_templateDate, 1);
        }

        _html += "</tr>" + System.getProperty("line.separator");
    }

    // HTML Boby close
    _html += "</table>" + System.getProperty("line.separator");

    // HTML Boby close
    _html += "</body>" + System.getProperty("line.separator");

    // Close document
    _html += "</html>";

    return _html;
}

// Returns the HEAD element
private String htmlHead() {
    String _htmlHead = "";
    _htmlHead += "<head>" + System.getProperty("line.separator");
    _htmlHead += htmlStyle();
    _htmlHead += "</head>" + System.getProperty("line.separator");

    return _htmlHead;
}

// Returns the STYLE element
private String htmlStyle() {
    String _htmlStyle = "";
    _htmlStyle += "<style>" + System.getProperty("line.separator");
    _htmlStyle += "html" + System.getProperty("line.separator");
    _htmlStyle += "{" + System.getProperty("line.separator");
    _htmlStyle += "font-family:'Arial';"
            + System.getProperty("line.separator");
    _htmlStyle += "}" + System.getProperty("line.separator");
    _htmlStyle += "table" + System.getProperty("line.separator");
    _htmlStyle += "{" + System.getProperty("line.separator");
    _htmlStyle += "border-collapse:collapse;"
            + System.getProperty("line.separator");
    _htmlStyle += "}" + System.getProperty("line.separator");
    _htmlStyle += "table, th, td" + System.getProperty("line.separator");
    _htmlStyle += "{" + System.getProperty("line.separator");
    _htmlStyle += "border: 1px solid black;"
            + System.getProperty("line.separator");
    _htmlStyle += "}" + System.getProperty("line.separator");
    _htmlStyle += "th, td" + System.getProperty("line.separator");
    _htmlStyle += "{" + System.getProperty("line.separator");
    _htmlStyle += "padding: 0px 10px 0px 10px;"
            + System.getProperty("line.separator");
    _htmlStyle += "}" + System.getProperty("line.separator");
    _htmlStyle += ".col_ {  background-color:#00ff00; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_0 { background-color:#00B050; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_1 { background-color:#33BB5A; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_2 { background-color:#66C764; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_3 { background-color:#99D36F; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_4 { background-color:#CCDF79; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_5 { background-color:#FFEB84; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_6 { background-color:#FFBD6A; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_7 { background-color:#FF8E50; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_8 { background-color:#FF5E35; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_9 { background-color:#FF2F1B; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += ".col_10 { background-color:#FF0000; text-align:right; }"
            + System.getProperty("line.separator");
    _htmlStyle += "</style>" + System.getProperty("line.separator");

    return _htmlStyle;
}
public String ReturnHeatMapHTML(){
//
CalculateStartEnd();
字符串_html=”“+System.getProperty(“line.separator”);
//HTML头
_html+=htmlHead();
//HTML正文打开
_html+=“”+System.getProperty(“line.separator”);
//HTML正文打开
_html+=“”+System.getProperty(“line.separator”);
//标题行
_html+=“00:0001:0002:0003:0004:0005:0006:0007:0008:0009:0010:0011:0012:0013:0014:0015:0016:0017:0018:0019:0020:0021:0022:0023:00”
+System.getProperty(“line.separator”);
//一天
DateFormat DateFormat=新的SimpleDateFormat(“EEE”,Locale.US);
对于(整数天=0;整数天<7;整数天++){
_html+=“”+dateFormat.format(addDays(\u startDate,\u days))
+ "";
Calendar calFrom=Calendar.getInstance();
calFrom.setTime(addDays(_startDate,_days));
日期_templateDate=新日期(calFrom.get(Calendar.YEAR)-1900,
calFrom.get(日历月),
calFrom.get(日历.月的第天),0,0,0);
对于(整数小时=0;整数小时<24;整数小时++){
字符串val=返回可用性(_templateDate);
_html+=“”+val+“”;
_templateDate=addHours(_templateDate,1);
}
_html+=“”+System.getProperty(“line.separator”);
}
//HTML波比关闭
_html+=“”+System.getProperty(“line.separator”);
//HTML波比关闭
_html+=“”+System.getProperty(“line.separator”);
//关闭文档
_html+=“”;
返回html;
}
//返回HEAD元素
私有字符串htmlHead(){
字符串_htmlHead=“”;
_htmlHead+=“”+System.getProperty(“line.separator”);
_htmlHead+=htmlStyle();
_htmlHead+=“”+System.getProperty(“line.separator”);
返回头;
}
//返回样式元素
私有字符串htmlStyle(){
字符串_htmlStyle=“”;
_htmlStyle+=“”+System.getProperty(“line.separator”);
_htmlStyle+=“html”+System.getProperty(“line.separator”);
_htmlStyle+=“{”+System.getProperty(“line.separator”);
_htmlStyle+=“字体系列:'Arial';”
+System.getProperty(“line.separator”);
_htmlStyle+=“}”+System.getProperty(“line.separator”);
_htmlStyle+=“table”+System.getProperty(“line.separator”);
_htmlStyle+=“{”+System.getProperty(“line.separator”);
_htmlStyle+=“边框折叠:折叠;”
+System.getProperty(“line.separator”);
_htmlStyle+=“}”+System.getProperty(“line.separator”);
_htmlStyle+=“table,th,td”+System.getProperty(“line.separator”);
_htmlStyle+=“{”+System.getProperty(“line.separator”);
_htmlStyle+=“边框:1px纯黑;”
+System.getProperty(“line.separator”);
_htmlStyle+=“}”+System.getProperty(“line.separator”);
_htmlStyle+=“th,td”+System.getProperty(“line.separator”);
_htmlStyle+=“{”+System.getProperty(“line.separator”);
_htmlStyle+=“填充:0px 10px 0px 10px;”
+System.getProperty(“line.separator”);
_htmlStyle+=“}”+System.getProperty(“line.separator”);
_htmlStyle+=“.col_{背景色:#00ff00;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_0{背景色:#00B050;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_1{背景色:#33BB5A;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_2{背景色:#66C764;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_3{背景色:#99D36F;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_4{背景色:#CCDF79;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_5{背景色:#FFEB84;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_6{背景色:#FFBD6A;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_7{背景色:#FF8E50;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_8{背景色:#FF5E35;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_9{背景色:#FF2F1B;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“.col_10{背景色:#FF0000;文本对齐:右;}”
+System.getProperty(“line.separator”);
_htmlStyle+=“”+System.getProperty(“line.separator”);
返回_htmlStyle;
}

我不明白你为什么要在Android中使用CSS。Android在设计布局时比CSS具有更大的灵活性和可维护性


我想你应该看看这个:

当然,我一发布这个,就发现了WebView。在这种情况下,webview完全按照需要渲染所有内容。为这个简单的问题和简单的回答道歉