简体   繁体   English

在Windows Phone 8.1 App中对齐按钮

[英]Align buttons in a windows Phone 8.1 App

I am trying to align 4 buttons on a window. 我正在尝试在窗口上对齐4个按钮。 Currently the window looks like this: 当前窗口如下所示: 在此处输入图片说明

We see that first two buttons are of same size and image and text are aligned. 我们看到前两个按钮的大小相同,并且图像和文本对齐。 Since the text in the third and fourth button ie Settings & About is smaller than the previous two, the buttons became smaller and also image is not aligned with the first two buttons. 由于第三个和第四个按钮(即“设置”和“关于”)中的文本比前两个小,因此这些按钮变得更小,并且图像也与前两个按钮不对齐。 How can I increase the size or align the third and fourth buttons. 如何增加尺寸或对齐第三个和第四个按钮。 My current XAML is: 我当前的XAML是:

<Grid Background="#FF89A8D4">
    <Grid.RowDefinitions>
        <RowDefinition Height="0.25*" />
        <RowDefinition Height="0.25*"/>
        <RowDefinition Height="0.25*"/>
        <RowDefinition Height="0.25*" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FFF16767" BorderThickness="2.5">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.25*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Image Source="/Assets/new.png"  Grid.Column="0" Margin="10"/>
            <TextBlock Text="Create Note"   Grid.Column="1" FontSize="30" Margin="15" FontFamily="Vijaya" Foreground="White"/>
        </Grid>
    </Button>
    <Button Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FF6060EC" BorderThickness="2.5">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.25*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Image Source="/Assets/search.png" Grid.Column="0" Margin="10"/>
            <TextBlock Text="Search Note" Grid.Column="1" FontSize="30" Margin="15" FontFamily="Vijaya" Foreground="White"/>
        </Grid>
    </Button>

    <Button Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FFB09F9F" BorderThickness="2.5">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.25*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Image Source="/Assets/settings.png"  Grid.Column="0" Margin="10"/>
            <TextBlock Text="Settings"   Grid.Column="1" FontSize="30" Margin="15" FontFamily="Vijaya" Foreground="White"/>
        </Grid>
    </Button>

    <Button Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FF899E3F" BorderThickness="2.5">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.25*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Image Source="/Assets/about.png"  Grid.Column="0" Margin="10"/>
            <TextBlock Text="About  "   Grid.Column="1" FontSize="30" Margin="15" FontFamily="Vijaya" Foreground="White"/>
        </Grid>
    </Button>
</Grid>

With your layout*), the easiest way is: 使用您的布局*),最简单的方法是:

  • for all <Button> s: 对于所有<Button>
    • leave HorizontalAlignment = Center 离开HorizontalAlignment = Center
    • set Width to some identical value, like 160 Width设置为相同的值,例如160

Smarter: 智慧:

  • for all <Button> s: 对于所有<Button>
    • change HorizontalAlignment to Stretch HorizontalAlignment更改为Stretch
    • set their Margin 's left and right to some identical value, like 10,0,10,0 将其Margin的left和right设置为相同的值,例如10,0,10,0

Or, if you don't like setting that margins per-button (or any other per-button) settings, you may put into Grid.Resources a Style with some Setter s that will set all the buttons. 或者,如果您不喜欢为每个按钮(或任何其他每个按钮)设置边距,则可以放入Grid.Resources一个带有一些SetterStyle来设置所有按钮。

Or if you don't want to set Margins on buttons, you can: 或者,如果您不想在按钮上设置边距,则可以:

  • for all <Button> s: 对于所有<Button>
    • change HorizontalAlignment to Stretch HorizontalAlignment更改为Stretch
  • for the Grid that holds them: 对于持有它们的Grid
    • set its Margin 's left and right to some identical value, like 10,0,10,0 将其Margin的left和right设置为相同的值,例如10,0,10,0

but the last option assumes that there is something around that Grid that obeys Margins. 但是最后一个选项假设网格周围有遵守边距的东西。 Usually it's true, but who knows what's in your app. 通常情况是对的,但是谁知道您的应用程序中包含什么。

Actually, I encourage you to try and play around with all those options. 实际上,我鼓励您尝试使用所有这些选项。 All of them look similar, but they mean a bit different things. 它们看起来都很相似,但是含义却有所不同。 It'd be good for you to understand/feel the difference, as it will make layouting in WPF much easier for you later. 最好理解/感觉到差异,因为这将使您以后在WPF中更轻松地进行布局。

Disclaimer: there are other ways to achieve similar result.. I didn't try to enumerate all of them, just the simplest/mostobvious ones. 免责声明:还有其他方法可以达到类似的结果。.我并未尝试列举所有方法,只是最简单/最显而易见的方法。

*) the contents of the buttons will automatically align to each other, because content definitions of each button contains the grid with the same shape: 0.25*/1.0* columns. *)按钮的内容将自动彼此对齐,因为每个按钮的内容定义包含具有相同形状的网格: 0.25*/1.0*列。 Once all buttons have the same outer dimensions, the inner columns will split the space in the same way and it will give same dimensions of the images and texts. 一旦所有按钮都具有相同的外部尺寸,内部列将以相同的方式分割空间,并且将赋予图像和文本相同的尺寸。 Of course assuming that there is enough space to satisfy each element's MinWidth/MinHeight/etc. 当然,假设有足够的空间来满足每个元素的MinWidth / MinHeight / etc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM