示例#1
0
    /* 获取农历字符串子程序 */
    private void setText(){
        synchronized (Main.class) {
            for (TextView dateView : mDateViews) {
        /* 判断当前日期栏是否包含上次更新后的日期文本
         * 如果当前日期已经改变,则必须重新计算农历
         * 如果当前日期未改变,则只需要重新用已经缓存的文本写入TextView */
                //判断日期是否改变,不改变则不更新内容,改变则重新计算农历
                String nDate = dateView.getText().toString();
                String lDate = (String) XposedHelpers.getAdditionalInstanceField(dateView, "lDate");
                String lunarText = (String) XposedHelpers.getAdditionalInstanceField(dateView, "lunarText");
                String finalText = (String) XposedHelpers.getAdditionalInstanceField(dateView, "finalText");
                if (lunarText == null || !(nDate.contains(lunarText) || finalText == null || nDate.equals(finalText))) {
                    if (lDate == null || !nDate.equals(lDate)) {
                        //获取时间
                        lunar.init();

                        //修正layout的singleLine属性
                        if (!layout_run) {
                            try {
                                //去掉singleLine属性
                                if (_layout_line)
                                    dateView.setSingleLine(false); //去除singleLine属性
                                //去掉align_baseline,并将其设置为center_vertical
                                if (_layout_align) {
                                    //一般机型的状态栏都是RelativeLayout,少数为LinearLayout,但似乎影响不大
                                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) dateView.getLayoutParams();
                                    layoutParams.addRule(RelativeLayout.ALIGN_BASELINE, 0); //去除baseline对齐属性
                                    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL); //并将其设置为绝对居中
                                    dateView.setLayoutParams(layoutParams); //设置布局参数
                                }
                                //设置宽度为wrap_content
                                if (_layout_width) {
                                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) dateView.getLayoutParams();
                                    layoutParams.width = LayoutParams.WRAP_CONTENT; //取消宽度限制
                                    dateView.setLayoutParams(layoutParams);
                                }
                                //设置高度为wrap_content
                                if (_layout_height) {
                                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) dateView.getLayoutParams();
                                    layoutParams.height = LayoutParams.WRAP_CONTENT; //取消高度限制
                                    dateView.setLayoutParams(layoutParams);
                                }
                                layout_run = true; //已经执行过布局的处理步骤,下次不再执行
                            } catch (Throwable t) {
                                XposedBridge.log("xStatusBarLunarDate: Statusbar layout fix error:");
                                XposedBridge.log(t);
                            }
                        }

                        //更新记录的日期
                        XposedHelpers.setAdditionalInstanceField(dateView, "lDate", nDate);
                        //从Lunar类中获得组合好的农历日期字符串(包括各节日)
                        lunarText = lunar.getFormattedDate(_custom_format, _format);
                        XposedHelpers.setAdditionalInstanceField(dateView, "lunarText", lunarText);

                        //重置提醒次数
                        _notify_times = _notify_times_setting;
                        if (_notify > 1) {
                            //当天是否是节日
                            isFest = !"".equals(lunar.getFormattedDate("ff", 5));
                            if ((isFest || _notify == 2) && _lang != 3) {
                                lunarTextToast = nDate.trim().replaceAll("\\n", " ") + "\n" + (_format == 5 ? lunar.getFormattedDate("", 3) : lunarText);
                            } else {
                                lunarTextToast = "";
                            }
                        }
                        //组合最终显示的文字
                        finalText = _remove_all ? lunarText : (_remove ? nDate.trim().replaceAll("[\\n|\\r]", " ") : nDate) + (_breakline ? "\n" : " ") + lunarText;
                        XposedHelpers.setAdditionalInstanceField(dateView, "finalText", finalText);
                    }
                    //向TextView设置显示的最终文字
                    dateView.setText(finalText);
                }
            }
        }
    }