LVGL_基础控件checkbox
1、创建checkbox
![LVGL_基础控件checkbox_第1张图片](http://img.e-com-net.com/image/info8/fa84c5b909e64da9bdf48404060709ba.jpg)
lv_obj_t * cb = lv_checkbox_create(lv_scr_act());
lv_checkbox_set_text(cb, "100ASK LVGL Tutorial" LV_SYMBOL_PLAY);
lv_obj_center(cb);
2、修改勾选框和文字之间的距离
![LVGL_基础控件checkbox_第2张图片](http://img.e-com-net.com/image/info8/72ab0bd62de14514aecdd8b9ddfc4fd7.jpg)
lv_obj_set_style_pad_column(cb, 100, 0);
3、修改勾选框和文字的大小
lv_obj_set_style_text_font(cb, &lv_font_montserrat_32, 0);
lv_obj_set_style_pad_all(cb, 20, LV_PART_INDICATOR);
4、修改复选框的背景颜色
![LVGL_基础控件checkbox_第3张图片](http://img.e-com-net.com/image/info8/24c5bf221cd84d4fa4b4dfadf251f1e9.jpg)
lv_obj_set_style_bg_opa(cb, 100, LV_PART_MAIN);
lv_obj_set_style_bg_color(cb, lv_color_hex(0xc43e1c), LV_PART_MAIN);
5、修改复选框选中和未选中时候的颜色
![LVGL_基础控件checkbox_第4张图片](http://img.e-com-net.com/image/info8/33853b9c2a7b4b41a2f6d5c242ebbe91.jpg)
![LVGL_基础控件checkbox_第5张图片](http://img.e-com-net.com/image/info8/8b109e2210294cabb40a153c5b52ff6f.jpg)
lv_obj_set_style_bg_color(cb, lv_color_hex(0xc43e1c), LV_PART_INDICATOR);
lv_obj_set_style_bg_color(cb, lv_color_hex(0x7719aa), LV_PART_INDICATOR | LV_STATE_CHECKED);
6、设置默认选中切不能更改
![LVGL_基础控件checkbox_第6张图片](http://img.e-com-net.com/image/info8/906ec8e448fc4f2aaaf55177bcb1dae1.jpg)
lv_obj_add_state(cb, LV_STATE_CHECKED);
lv_obj_add_state(cb, LV_STATE_CHECKED | LV_STATE_DISABLED);
7、设置默认不选中且不能更改
![LVGL_基础控件checkbox_第7张图片](http://img.e-com-net.com/image/info8/1f576e197e064d01aea4379872b121ce.jpg)
lv_obj_clear_state(cb, LV_STATE_CHECKED);
lv_obj_add_state(cb, LV_STATE_DISABLED);
8、清除禁用状态
lv_obj_clear_state(cb, LV_STATE_DISABLED);
9、添加事件
lv_obj_add_event_cb(cb, cb_event_handler, LV_EVENT_VALUE_CHANGED, NULL);
static void cb_event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t *cb = lv_event_get_target(e);
if (code == LV_EVENT_VALUE_CHANGED)
{
const char * txt = lv_checkbox_get_text(cb);
LV_LOG_USER("%s: %s", txt, lv_obj_has_state(cb, LV_STATE_CHECKED) ? "CHECKED" : "UNCHECKED");
}
}