三种基础的布局控件
来源:互联网 发布:网络协议实践实验报告 编辑:程序博客网 时间:2024/05/17 07:16
布局控件对于用户体验来说至关重要,下面就来体验一下Windows8的应用商店项目开发中的几种常用布局吧。
新建一个项目叫做LayoutTest来做测试。
一:Grid网格布局控件
作用:定义由行和列组成的网格区域。新建一个空白xaml页面,命名为:GridLayout.xaml。
里面写上如下代码:
- <Page
- x:Class="LayoutTest.GridLayout"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:LayoutTest"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
- <!--Grid表格布局
- Grid.RowDefinitions:定义Grid中的行
- Grid.ColumnDefinitions :定义Grid的列
- 创建一个四行五列的表格布局
- -->
- <Grid HorizontalAlignment="Center" Height="210" VerticalAlignment="Center" Width="305">
- <Grid.RowDefinitions>
- <!--定义四行及每行高度,*表示所占的比例-->
- <RowDefinition Height="1*"/>
- <RowDefinition Height="2*"/>
- <RowDefinition Height="3*"/>
- <RowDefinition Height="4*"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <!--定义五列及每列宽度,*表示所占的比例-->
- <ColumnDefinition Width="1*" />
- <ColumnDefinition Width="2*"/>
- <ColumnDefinition Width="3*"/>
- <ColumnDefinition Width="4*"/>
- <ColumnDefinition Width="5*"/>
- </Grid.ColumnDefinitions>
- </Grid>
- </Page>
这样便是一个简单的格子布局,效果如图所示:
这样只是简单的创建了格子布局,为了让效果更明显一点,我们把每个格子都填充上颜色,完整代码如下:
- <Page
- x:Class="LayoutTest.GridLayout"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:LayoutTest"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
- <!--Grid表格布局
- Grid.RowDefinitions:定义Grid中的行
- Grid.ColumnDefinitions :定义Grid的列
- 创建一个四行五列的表格布局
- -->
- <Grid HorizontalAlignment="Center" Height="210" VerticalAlignment="Center" Width="305">
- <Grid.RowDefinitions>
- <!--定义四行及每行高度,*表示所占的比例-->
- <RowDefinition Height="1*"/>
- <RowDefinition Height="2*"/>
- <RowDefinition Height="3*"/>
- <RowDefinition Height="4*"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <!--定义五列及每列宽度,*表示所占的比例-->
- <ColumnDefinition Width="1*" />
- <ColumnDefinition Width="2*"/>
- <ColumnDefinition Width="3*"/>
- <ColumnDefinition Width="4*"/>
- <ColumnDefinition Width="5*"/>
- </Grid.ColumnDefinitions>
- <!--第一行的五列-->
- <Rectangle Fill="#000000" Grid.Row="0" Grid.Column="0" />
- <Rectangle Fill="#444444" Grid.Row="0" Grid.Column="1" />
- <Rectangle Fill="#888888" Grid.Row="0" Grid.Column="2" />
- <Rectangle Fill="#bbbbbb" Grid.Row="0" Grid.Column="3" />
- <Rectangle Fill="#ffffff" Grid.Row="0" Grid.Column="4" />
- <!--第二行的五列-->
- <Rectangle Fill="#000000" Grid.Row="1" Grid.Column="0" />
- <Rectangle Fill="#000044" Grid.Row="1" Grid.Column="1" />
- <Rectangle Fill="#000088" Grid.Row="1" Grid.Column="2" />
- <Rectangle Fill="#0000bb" Grid.Row="1" Grid.Column="3" />
- <Rectangle Fill="#0000ff" Grid.Row="1" Grid.Column="4" />
- <!--第三行的五列-->
- <Rectangle Fill="#000000" Grid.Row="2" Grid.Column="0" />
- <Rectangle Fill="#004400" Grid.Row="2" Grid.Column="1" />
- <Rectangle Fill="#008800" Grid.Row="2" Grid.Column="2" />
- <Rectangle Fill="#00bb00" Grid.Row="2" Grid.Column="3" />
- <Rectangle Fill="#00ff00" Grid.Row="2" Grid.Column="4" />
- <!--第四行的五列-->
- <Rectangle Fill="#000000" Grid.Row="3" Grid.Column="0" />
- <Rectangle Fill="#440000" Grid.Row="3" Grid.Column="1" />
- <Rectangle Fill="#880000" Grid.Row="3" Grid.Column="2" />
- <Rectangle Fill="#bb0000" Grid.Row="3" Grid.Column="3" />
- <Rectangle Fill="#ff0000" Grid.Row="3" Grid.Column="4" />
- </Grid>
- </Page>
二:Canvas画布布局
作用:定义一个区域可以使用相对于该区域的坐标直接定位子元素。
给工程添加一个新的空白xaml文件,叫做CanvasLayout.xaml。
完整的代码如下:
- <Page
- x:Class="LayoutTest.CanvasLayout"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:LayoutTest"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
- <Grid>
- <Canvas HorizontalAlignment="Center" Height="600" Margin="0"
- VerticalAlignment="Center" Width="800" Background="Blue">
- <!--Canvas里子元素通过调整Canvas区域的绝对位置来定位
- Canvas.Left – 以左上角为原点,Canvas X轴的距离
- Canvas.Top – 以左上角为原点,Canvas Y轴的距离
- -->
- <Ellipse Fill="Red" Height="51" Canvas.Left="400" Stroke="White"
- Canvas.Top="52" Width="53"/>
- <Rectangle Fill="Green" Height="56" Canvas.Left="415" Stroke="Black"
- Canvas.Top="105" Width="20"/>
- </Canvas>
- </Grid>
- </Page>
效果图:
三、StackPanel排列布局控件
作用:将子元素排列成一行(可沿水平或垂直方向)
再添加一个空白页,命名为“StackPanelLayout.xaml”,然后把原来的Grid标签删除,在Page标签内添加如下代码:
- <Page
- x:Class="LayoutTest.StackPanelLayout"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:LayoutTest"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
- <StackPanel Background="Black">
- <StackPanel Orientation="Horizontal" x:Name="stackpanel">
- <Button>Test</Button>
- <Button>Test</Button>
- <Button>Test</Button>
- <Button>Test</Button>
- <Button>Test</Button>
- <Button>Test</Button>
- <Button>Test</Button>
- <Button>Test</Button>
- <Button>Test</Button>
- <Button>Test</Button>
- <Button>Test</Button>
- </StackPanel>
- <Button Width="111" Height="111">变换方向</Button>
- </StackPanel>
- </Page>
此时可以看到一排按钮齐刷刷的排在那里,那么接下来为这个按钮添加监听,监听代码如下:
- private void Change(object sender, RoutedEventArgs e)
- {
- ///更改Stackpanel控件的内部排列方式
- if (stackpanel.Orientation == Orientation.Horizontal)
- {
- stackpanel.Orientation = Orientation.Vertical;
- }
- else
- {
- stackpanel.Orientation = Orientation.Horizontal;
- }
- }
打开App.xaml.cs,找到下面这几句代码,把typeof括号里的MainPage改成StackPanelLayout,这里的意思就是应用启动时候显示的页面。
运行一下,便可以看到风骚的效果了。
0 0
- 三种基础的布局控件
- [Win8]Windows8开发笔记(二):三种基础的布局控件
- Android基础—小白篇(三):为自己的APK描眉画眼——布局And控件(布局篇)
- UI基础__控件布局的几种方法总结
- Blend基础-布局控件
- android基础控件,布局
- 【JAVA语言程序设计基础篇】--图形用户界面基础--三种基本的布局管理器
- html的三种布局
- Android 各种基础控件布局
- 【Android 基础】Google新推出的Android布局控件FlexboxLayout
- 三列布局的三种方法
- 响应式布局基础三:webapp布局
- 元素的三种展示类型和两种基础布局方式
- 相对布局的控件布局
- Extjs4循序渐进(三)——表单及表单控件详解一(表单布局和基础控件 Text,Text...
- 2016-04-25Andriod基础 三种布局
- DevExpress控件的布局
- 控件的布局动画
- Cocos国外大牛分享次世代画质3D案例
- FlipView和自定义值转换器
- Java语言描述:回溯法之最小重量机器问题
- lua发http请求
- 用protoc-gen-lua生成PB的lua代码
- 三种基础的布局控件
- 页面导航的基础与深入
- shell获取mac地址
- 在lua环境中使用protobuf
- 文件访问和选取器
- JSF框架
- [C#基础]线程同步技术之Join方法
- android_基本控件学习当中出现的问题总结及工作问题思考
- 【Android开发】范例1-开启新线程获取网络图片并显示到ImageView中