KivyMD自定义MDDialog内容重叠问题的解决方案
MDDialog内容重叠问题解决方案
问题说明
在使用KivyMD开发时遇到MDDialog内所有内容重叠的问题:尝试在KV文件的GridLayout中添加adaptive_height属性后,重叠情况反而更加严重,切换其他布局又会出现控件定位困难的问题。
点击主屏幕按钮唤起设置弹窗后,弹窗底部的操作按钮会与内容区的开关控件重叠。
相关代码
KV代码
<Settings_Dialog> rows: 4 adaptive_height: True MDLabel: text: 'App Settings' font_size: 15 MDGridLayout: cols: 2 rows: 1 MDLabel: text: 'App Theme' font_size: 10 ToggleButton: id: darklight text: {'normal':'LIGHT', 'down':'DARK'}[self.state] color: {'normal':[0,0,0], 'down':[1,1,1]}[self.state] background_color: {'normal':[1,1,1,1], 'down':[0,0,0,1]}[self.state] font_size: self.height * .6 on_press: print(self.state) size_hint: 0.2, 0.8 MDLabel: text: 'Plot Settings' font_size: 15 MDGridLayout: rows: 3 cols: 2 MDLabel: text: "Plot Type" font_size: 10 MDDropDownItem: id: g_type text: 'Candle' font_size: 10 #current_item: 'Candle' on_release: root.plot_type.open() MDLabel: text: 'Plot Color' font_size: 10 MDDropDownItem: id: g_color text: 'Default' font_size: 10 #current_item: 'Default' on_release: root.plot_color.open() MDLabel: text: 'Volume' font_size: 10 MDSwitch: active: True size_hint: 0.3,0.8
Python代码片段
class MainScreen(MDScreen): def settings(self): close_button = MDFlatButton( text = 'Close', font_size = 15, on_release = self.close_settings, opacity = 0.5 ) save_button = MDFlatButton( text = 'Save Changes', font_size = 15, on_release = self.savee, opacity = 0.5 ) self.settings = MDDialog( title = 'Settings', auto_dismiss = False, type = 'custom', content_cls = Settings_Dialog(), buttons = [save_button, close_button] ) self.settings.open() def close_settings(self, obj): self.settings.dismiss() def savee(self, obj): self.settings.dismiss() class Settings_Dialog(MDGridLayout): ## 此处有若干功能函数,均不涉及控件尺寸相关设置
修复方案
只需给弹窗内所有控件添加adaptive_height: True属性,即可消除内容重叠问题。
内容的提问来源于stack exchange,提问作者I3lackI3uck
相关产品推荐
相关产品推荐

