lvgl 滑动切换界面
时间: 2023-09-02 13:12:02 浏览: 273
在lvgl中,我们可以使用`lv_tabview`控件来实现滑动切换界面的效果。
1. 创建一个`lv_tabview`控件:
```c
lv_obj_t *tabview = lv_tabview_create(lv_scr_act(), NULL);
```
2. 添加选项卡:
```c
lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Tab 1");
lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Tab 2");
```
3. 在每个选项卡中添加控件:
```c
// 在Tab 1中添加一个标签
lv_obj_t *label1 = lv_label_create(tab1, NULL);
lv_label_set_text(label1, "This is Tab 1");
// 在Tab 2中添加一个按钮
lv_obj_t *button2 = lv_btn_create(tab2, NULL);
lv_obj_t *label2 = lv_label_create(button2, NULL);
lv_label_set_text(label2, "Button in Tab 2");
```
4. 最后,调用`lv_tabview_set_sliding(tabview, true)`开启滑动切换界面的功能:
```c
lv_tabview_set_sliding(tabview, true);
```
完整示例代码:
```c
lv_obj_t *tabview = lv_tabview_create(lv_scr_act(), NULL);
lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Tab 1");
lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Tab 2");
lv_obj_t *label1 = lv_label_create(tab1, NULL);
lv_label_set_text(label1, "This is Tab 1");
lv_obj_t *button2 = lv_btn_create(tab2, NULL);
lv_obj_t *label2 = lv_label_create(button2, NULL);
lv_label_set_text(label2, "Button in Tab 2");
lv_tabview_set_sliding(tabview, true);
```
阅读全文