<
GridLayout
columns
=
"auto,auto"
rows
=
"auto"
>
<
Label
col
=
"0"
text
=
"hello"
/>
<
Button
col
=
"1" text="ohai"
/>
</
GridLayout
>
var
layout = require(
"ui/layouts/grid-layout"
);
var
buttonModule = require(
"ui/button"
);
var
labelModule = require(
"ui/label"
);
exports.loaded =
function
(args) {
var
page = args.object;
var
gridLayout =
new
layout.GridLayout();
var
button1 =
new
buttonModule.Button();
var
label1 =
new
labelModule.Label();
button1.text =
"hello"
;
label1.text =
"ohai"
;
layout.GridLayout.setColumn(button1, 0);
layout.GridLayout.setColumn(label1, 1);
gridLayout.addChild(button1);
gridLayout.addChild(label1);
var
firstColumn =
new
layout.ItemSpec(1, layout.GridUnitType.auto);
var
secondColumn =
new
layout.ItemSpec(1, layout.GridUnitType.auto);
var
firstRow =
new
layout.ItemSpec(1, layout.GridUnitType.auto);
gridLayout.addColumn(firstColumn);
gridLayout.addColumn(secondColumn);
gridLayout.addRow(firstRow);
page.content = gridLayout;
};
ItemSpec
是一个类,用于指定行高和列宽的值和类型。您可以选择如何编写布局代码 - 使用 XML 可能稍微简短一些且更容易,但两种方式都可以。“基本概念是我们有不同的布局视图(或布局容器),它们可以有一个或多个子视图。每个布局容器都有自己的逻辑,用于在其获得的空间内排列其子元素。”以下是布局类型及其流程的列表
<tr>
和 <td>
标签。创建网格并使用rowSpans
和 colSpans
向其中添加子元素,类似于 HTML 中表格的标记。StackLayout
将一个项目垂直地叠加在另一个项目上。标记将很简单<
StackLayout
orientation
=
"vertical"
>
<
Button
text
=
"one"
/>
<
Button
text
=
"two"
/>
<
Button
text
=
"three"
/>
<
Button
text
=
"four"
/>
<
Button
text
=
"five"
/>
</
StackLayout
>
orientation="horizontal"
水平堆叠,相同的标记将简单地水平对齐<
StackLayout
orientation
=
"horizontal"
horizontalAlignment
=
"center"
>
<
StackLayout
orientation
=
"horizontal"
horizontalAlignment
=
"center"
verticalAlignment
=
"center"
minHeight
=
"100"
>
<
StackLayout
orientation
=
"vertical"
>
<
Button
text
=
"one"
horizontalAlignment
=
"left"
minWidth
=
"100"
/>
<
Button
text
=
"two"
horizontalAlignment
=
"center"
minWidth
=
"30"
/>
<
Button
text
=
"three"
horizontalAlignment
=
"right"
minWidth
=
"70"
/>
<
Button
text
=
"four"
/>
</
StackLayout
>
ListView
等小部件时,请确保将父网格的高度设置为“star”,否则它会被截断。另一方面,在使用图像时,您可以通过设置其 stretch
属性(如下所示)来设置图像本身以适应其可用空间。注意:默认情况下,GridLayout 的行为类似于具有“star”高度和宽度的单行单列。您需要相应地规划此类布局与小部件控件子元素的使用。
<
GridLayout
rows
=
"auto,auto"
columns
=
"auto,auto"
>
<
Label
text
=
"one"
row
=
"0"
col
=
"0"
/>
<
Label
text
=
"two"
row
=
"0"
col
=
"1"
/>
<
Label
text
=
"three"
row
=
"1"
col
=
"0"
/>
<
Label
text
=
"four"
row
=
"1"
col
=
"1"
/>
</
GridLayout
>
horizontalAlignment
属性可以使网格看起来更好一些<
GridLayout
rows
=
"auto,auto"
columns
=
"auto,auto"
horizontalAlignment
=
"center"
>
<
Label
text
=
"one"
row
=
"0"
col
=
"0"
horizontalAlignment
=
"left"
/>
<
Label
text
=
"two"
row
=
"0"
col
=
"1"
horizontalAlignment
=
"right"
/>
<
Label
text
=
"three"
row
=
"1"
col
=
"0"
horizontalAlignment
=
"left"
/>
<
Label
text
=
"four"
row
=
"1"
col
=
"1"
horizontalAlignment
=
"right"
/>
</
GridLayout
>
colSpan
以跨越网格底部,可以让您更精细地控制内容<
GridLayout
rows
=
"60,60,auto"
columns
=
"60,60"
horizontalAlignment
=
"center"
verticalAlignment
=
"center"
>
<
Button
text
=
"red"
row
=
"0"
col
=
"0"
cssClass
=
"red"
/>
<
Button
text
=
"blue"
row
=
"0"
col
=
"1"
cssClass
=
"blue"
/>
<
Button
text
=
"yellow"
row
=
"1"
col
=
"0"
cssClass
=
"yellow"
/>
<
Button
text
=
"green"
row
=
"1"
col
=
"1"
cssClass
=
"green"
/>
<
Label
text
=
"pick a color!"
row
=
"2"
colSpan
=
"2"
horizontalAlignment
=
"center"
/>
</
GridLayout
>
bike.png
[email protected](bike.png 的两倍大小)
[email protected](bike.png 的三倍大小,以支持视网膜显示屏)
stretch
属性
<
StackLayout
orientation
=
"vertical"
horizontalAlignment
=
"center"
>
<
Image
src
=
"res://bike"
stretch
=
"none"
/>
<
Image
src
=
"res://bike"
stretch
=
"none"
/>
<
Image
src
=
"res://bike"
stretch
=
"none"
/>
<
Image
src
=
"res://bike"
stretch
=
"none"
/>
<
Image
src
=
"res://bike"
stretch
=
"none"
/>
</
StackLayout
>
stretch
属性可以是四个值之一<
GridLayout
columns
=
"*,*,*,*"
rows
=
"auto, *, *"
>
<!--此区域是顶部标题栏-->
<
GridLayout
class
=
"header-container"
colSpan
=
"5"
columns
=
"*,*,*,*,*"
>
<
Label
text
=
""
horizontalAlignment
=
"left"
verticalAlignment
=
"center"
tap
=
"refresh"
class
=
"top-icon weather-icon small-icon"
/>
<
Label
text
=
"My Weather"
colSpan
=
"5"
horizontalAlignment
=
"center"
verticalAlignment
=
"center"
class
=
"large-text"
/>
<
ActivityIndicator
col
=
"3"
busy
=
"{{ isLoading }}"
horizontalAlignment
=
"right"
/>
<
Label
text
=
""
col
=
"4"
horizontalAlignment
=
"right"
verticalAlignment
=
"center"
class
=
"top-icon weather-icon small-icon"
tap
=
"openInfo"
/>
</
GridLayout
>
<!--天气信息容器,两列基于网格的列,每列中都包含一个堆叠布局-->
<
StackLayout
class
=
"blue1-container"
colSpan
=
"2"
row
=
"1"
>
<
Label
text
=
"Now"
row
=
"2"
colSpan
=
"2"
horizontalAlignment
=
"center"
verticalAlignment
=
"center"
class
=
"large-text top"
/>
<
Label
row
=
"3"
horizontalAlignment
=
"center"
colSpan
=
"2"
text
=
""
class
=
"weather-icon large-icon"
/>
<
Label
text
=
"77°"
row
=
"4"
colSpan
=
"2"
horizontalAlignment
=
"center"
class
=
"large-text"
/>
</
StackLayout
>
<
StackLayout
class
=
"blue3-container"
colSpan
=
"2"
row
=
"1"
col
=
"2"
>
<
Image
stretch
=
"none"
row
=
"2"
col
=
"2"
colSpan
=
"2"
src
=
"res://bike"
horizontalAlignment
=
"center"
class
=
"top"
verticalAlignment
=
"center"
/>
<
Label
row
=
"3"
horizontalAlignment
=
"center"
colSpan
=
"2"
text
=
""
class
=
"weather-icon large-icon"
/>
<
Label
text
=
"77°"
row
=
"4"
col
=
"2"
colSpan
=
"2"
horizontalAlignment
=
"center"
class
=
"large-text"
/>
</
StackLayout
>
<!--我们根据学生选择的出发时间显示穿衣建议的底部区域-->
<
Image
stretch
=
"aspectFit"
row
=
"2"
colSpan
=
"4"
src
=
"res://warmbg"
verticalAlignment
=
"bottom"
/>
</
GridLayout
>
<
GridLayout
class
=
"header-container"
colSpan
=
"5"
columns
=
"*,*,*,*,*"
>
<
Label
text
=
""
horizontalAlignment
=
"left"
verticalAlignment
=
"center"
tap
=
"refresh"
class
=
"top-icon weather-icon small-icon"
/>
<
Label
text
=
"My Weather"
colSpan
=
"5"
horizontalAlignment
=
"center"
verticalAlignment
=
"center"
class
=
"large-text"
/>
<
ActivityIndicator
col
=
"3"
busy
=
"{{ isLoading }}"
horizontalAlignment
=
"right"
/>
<
Label
text
=
""
col
=
"4"
horizontalAlignment
=
"right"
verticalAlignment
=
"center"
class
=
"top-icon weather-icon small-icon"
tap
=
"openInfo"
/>
</
GridLayout
>
<
StackLayout
class
=
"blue1-container"
colSpan
=
"2"
row
=
"1"
>
<
Label
text
=
"Now"
row
=
"2"
colSpan
=
"2"
horizontalAlignment
=
"center"
verticalAlignment
=
"center"
class
=
"large-text top"
/>
<
Label
row
=
"3"
horizontalAlignment
=
"center"
colSpan
=
"2"
text
=
""
class
=
"weather-icon large-icon"
/>
<
Label
text
=
"77°"
row
=
"4"
colSpan
=
"2"
horizontalAlignment
=
"center"
class
=
"large-text"
/>
</
StackLayout
>
<
StackLayout
class
=
"blue3-container"
colSpan
=
"2"
row
=
"1"
col
=
"2"
>
<
Image
stretch
=
"none"
row
=
"2"
col
=
"2"
colSpan
=
"2"
src
=
"res://bike"
horizontalAlignment
=
"center"
class
=
"top"
verticalAlignment
=
"center"
/>
<
Label
row
=
"3"
horizontalAlignment
=
"center"
colSpan
=
"2"
text
=
""
class
=
"weather-icon large-icon"
/>
<
Label
text
=
"77°"
row
=
"4"
col
=
"2"
colSpan
=
"2"
horizontalAlignment
=
"center"
class
=
"large-text"
/>
</
StackLayout
>
<
Image
stretch
=
"aspectFit"
row
=
"2"
colSpan
=
"4"
src
=
"res://warmbg"
verticalAlignment
=
"bottom"
/>
注意选项卡布局在各种设备上的工作方式。这是一种很好的方法,可以观察 NativeScript 框架如何通过向用户提供在其所选平台上最熟悉的 UX 来创建真正的跨平台原生用户体验。