LVGL lv_checkbox复选框(14)

lv_checkbox

复选框由背景、左边的按钮btn、右边的文本组成,相关API在lv_checkbox.h

样式

/** Checkbox styles. */
enum {
    LV_CHECKBOX_PART_BG = LV_BTN_PART_MAIN,  /**< Style of object background. */
    _LV_CHECKBOX_PART_VIRTUAL_LAST,
    LV_CHECKBOX_PART_BULLET = _LV_BTN_PART_REAL_LAST, /**< Style of box (released). */
    _LV_CHECKBOX_PART_REAL_LAST
};
typedef uint8_t lv_checkbox_style_t;

例子

我没找到Unchecked时用什么样式来修改默认颜色

static void event_handler(lv_obj_t * obj, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        printf("State: %s\n", lv_checkbox_is_checked(obj) ? "Checked" : "Unchecked");
    }
}

void lv_ex_checkbox_1(void)
{
	static lv_style_t style;
	lv_style_init(&style);
	lv_style_set_bg_color(&style, LV_STATE_CHECKED, LV_COLOR_RED);
	lv_style_set_bg_color(&style, LV_STATE_PRESSED, LV_COLOR_RED);
	lv_style_set_border_color(&style, LV_STATE_DEFAULT, LV_COLOR_RED);
	lv_style_set_border_color(&style, LV_STATE_PRESSED, LV_COLOR_RED);

	static lv_style_t style_bg;
	lv_style_init(&style_bg);
	lv_style_set_outline_color(&style_bg, LV_STATE_DEFAULT, LV_COLOR_RED);

    lv_obj_t * cb = lv_checkbox_create(lv_scr_act(), NULL);
    lv_checkbox_set_text(cb, "I agree");

	lv_obj_add_style(cb, LV_CHECKBOX_PART_BULLET, &style);
	lv_obj_add_style(cb, LV_CHECKBOX_PART_BG, &style_bg);

    lv_obj_align(cb, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_event_cb(cb, event_handler);
}

LVGL lv_checkbox复选框(14)_第1张图片

你可能感兴趣的:(LVGL)