1

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

为了实现这一点,我使用 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;
}
4

1 回答 1

0

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

我认为你应该看看这个:http: //developer.android.com/guide/topics/ui/themes.html

于 2013-11-06T22:18:43.507 回答