問題描述
BoxLayout(orientation='vertical')
與 GridLayout(cols=1)
:
他們都做同樣的事情,不是嗎?有理由選擇其中一個嗎?
They both do the same thing, no? Is there a reason to choose one over the other?
推薦答案
區別在于大小和位置.
一般來說,GridLayout
(cols: 1
) 總是將元素保持在一列中,而有使用 BoxLayout
(orientation: 'vertical'
) 時更靈活地組織單個小部件.
In general, GridLayout
(cols: 1
) is always going to keep the elements in one column, whereas there is more flexibility to organize individual widgets when you use BoxLayout
(orientation: 'vertical'
).
這是一個非常簡單的示例,說明您可以使用 BoxLayout
進行操作,因為它支持 pos_hint
、size
和 size_hint
(以及其他,例如 center_x
、x
、y
、right
,-注意它們也依賴于BoxLayout
的 vertical
或 horizo??ntal
方向會影響各個小部件:
Here is a very simple example of something you can do with BoxLayout
because it honours pos_hint
, size
and size_hint
(and others such as center_x
, x
, y
, right
, - notice that they also depend on the vertical
or horizontal
orientation of the BoxLayout
) which affects individual widgets:
<Test@BoxLayout>:
orientation: 'vertical'
Button:
text: 'a'
size_hint: None, None
size: 100,50
pos_hint: { 'center_x' : .5 }
Button:
text: 'b'
這是 200x200 屏幕中的輸出:
This is the output in a 200x200 screen:
如果您嘗試做同樣的事情,但改用 GridLayout
,那么您會得到:
If you attempt to do the same but using GridLayout
instead, then you get this:
最后,GridLayout
有一些屬性來控制列的大小:
Finally, GridLayout
has some properties to control the size of the column:
col_default_width
:為所有列的默認寬度col_width
:每列的寬度列表(在此沒有用處案例,因為我們只有一個)col_force_default
:它將忽略單個小部件的任何現有size_hint
或size
并強制列寬minimum_width
:所以列不會縮小太多
col_default_width
: for the default width of all the columnscol_width
: a list of widths for each column (not useful in this case since we have just one)col_force_default
: which will ignore any existingsize_hint
orsize
for individual widgets and force the column widthminimum_width
: so the column not shrink too much
這篇關于Kivy:BoxLayout 與 GridLayout的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!