我正在尝试将我添加到StackLayout的GridLayout水平居中。 GridLayout的大小足以满足其内容,并且不会增长或缩小。我尝试将GridLayout添加到一个有用的AnchorLayout,但我无法弄清楚如何将AnchorLayout的高度设置为我添加到它的GridLayout的高度。
我尝试使用height: self.children[0].height
设置AnchorLayout高度,它不起作用(没有错误,但高度是默认值100)
如何将AnchorLayout高度设置为其孩子的高度? (或者有更好的方法来使GridLayout居中吗?这是我的主要目标。)
以下是示例kv代码:
<MyStackLayout>
Label:
text: 'header #1'
size_hint_y: None
height: self.texture_size[1]+2
AnchorLayout:
size_hint_y: None
height: self.children[0].height
GridLayout:
cols: 2
padding: 1,1,1,1
height: self.minimum_height
width: self.minimum_width
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
CheckBox:
size: 25, 25
size_hint: None, None
Label:
text: 'header #2'
size_hint_y: None
height: self.texture_size[1]+2
运行它的python代码:
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.stacklayout import StackLayout
class MyStackLayout(StackLayout):
pass
class ExampleApp(App):
def build(self):
return MyStackLayout()
if __name__ == '__main__':
ExampleApp().run()
我想要的效果是这样的:
<center>
<p>header 1</p>
<table>
<tr>
<td>
<input type='checkbox'>
</td>
<td>
<input type='checkbox'>
</td>
</tr>
<td>
<input type='checkbox'>
</td>
<td>
<input type='checkbox'>
</td>
</tr>
</table>
<p>header 2</p>
... more stuff here later
</center>
答案 0 :(得分:1)
感谢Nykakin,我基本上已经明白了。我在GridLayout上放了一个id,然后用它设置AnchorLayout的高度。
AnchorLayout:
size_hint_y: None
height: thegrid.height
GridLayout:
id: thegrid
#...