如何加粗字符串

时间:2015-10-01 06:03:17

标签: android spannablestring

我的应用程序中的ListView有许多字符串元素,如天,天名等。我只想让天变粗。我只想在我的应用程序中加粗日期。加粗字符串

我知道我可以在Spannable的帮助下加粗这样的文字。链接可以显示如下:

private String getHeaderDate(String dateString) {
    try {


        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        Date date = dateFormat.parse(dateString);

        SimpleDateFormat dayFormat = new SimpleDateFormat("EEE");
        SimpleDateFormat monthFormat = new SimpleDateFormat("MMM");

        String dayName = String.valueOf(dayFormat.format(date));
        String month = String.valueOf(monthFormat.format(date));

        String day = dateString.split("/")[1];

        Calendar calendarGivenDate = Calendar.getInstance();
       // 
Date d1 = calendarGivenDate.getTime();

        Date d1 = calendarGivenDate.getTime();
        Date date1 = new Date();
        String stringSecond = dateFormat.format(d1);
        //
String stringSecond = dateFormat.format(d1);


        Date dateLater = dateFormat.parse(dateString);
        Date dateCurrent = dateFormat.parse(stringSecond);
      //  Date dateCurrent = dateFormat.parse(dateString);

       // Date dateLater = dateFormat.parse(stringSecond);


        long difference = dateLater.getTime() - dateCurrent.getTime();
        long daysDiff = TimeUnit.DAYS.convert(difference, TimeUnit.MILLISECONDS);

       // long daysDiff = TimeUnit.DAYS.convert(difference, TimeUnit.MILLISECONDS);
        String days = "";

        if(daysDiff == 1) {
            days = "Tomorrow";
            String finalString= days;
            Spannable sb = new SpannableString( finalString );
            sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), finalString.indexOf(days), days.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        }

           else if(daysDiff == 0) {
            days = "Today";
            String finalString= days;
            Spannable sb = new SpannableString( finalString );
         sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), finalString.indexOf(days), days.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        }
//
        return StringUtils.join(days.equals("") ? "" : StringUtils.join(days, " ", "•", " ") , dayName, " ", month, " ", day);

    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

0 个答案:

没有答案