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

时间:2013-11-06 22:11:10

标签: android 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;
}

1 个答案:

答案 0 :(得分:0)

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

我认为你应该研究一下:http://developer.android.com/guide/topics/ui/themes.html