如何在StackLayout中定位3个按钮的宽度相等

时间:2017-11-17 19:03:55

标签: xamarin xamarin.forms

任何人都知道如何在StackLayout中以3个宽度放置3个按钮?我有它与Grid一起工作

<Grid x:Name="MyGrid" Grid.Row="0" BindingContext="{x:Reference Name=Button1}"  HeightRequest="{Binding Width}">

我想找到一种方法,在没有网格的情况下,在同一条线上显示相同宽度的3个按钮 例如,所有3个等长的按钮在StackLayout

中水平横跨

[按钮1] [按钮222] [按钮333333]

3 个答案:

答案 0 :(得分:11)

仍在使用网格,只需指定列的宽度(百分比设置的色列间距):

    <Grid ColumnSpacing="8" HorizontalOptions="FillAndExpand">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="33.33*" />
            <ColumnDefinition Width="33.33*" />
            <ColumnDefinition Width="33.33*" />
        </Grid.ColumnDefinitions>
        <Button Text="1" Grid.Column="0" HorizontalOptions="FillAndExpand"/>
        <Button Text="2" Grid.Column="1" HorizontalOptions="FillAndExpand"/>
        <Button Text="3" Grid.Column="2" HorizontalOptions="FillAndExpand"/>          
     </Grid>

enter image description here

答案 1 :(得分:1)

<StackLayout VerticalOptions="EndAndExpand" Orientation="Horizontal" Spacing="2" Padding="2">
            <Button Text="Move Up" HorizontalOptions="FillAndExpand" BackgroundColor="AntiqueWhite" TextColor="Black" />
            <Button Text="Move Down" HorizontalOptions="FillAndExpand" BackgroundColor="AntiqueWhite" TextColor="Black" />
            <Button Text="Move Right" HorizontalOptions="FillAndExpand" BackgroundColor="AntiqueWhite" TextColor="Black" />
            <Button Text="Move Left" HorizontalOptions="FillAndExpand" BackgroundColor="AntiqueWhite" TextColor="Black" />
</StackLayout>

好像是一个旧线程,但是对于任何想用StackLayout实现的人来说,这将使按钮以相等的间距准确地水平放置。

答案 2 :(得分:0)

RelativeLayout的:

<RelativeLayout HorizontalOptions="FillAndExpand">
    <Button Text="Button 1" RelativeLayout.XConstraint="{ConstraintExpression 
            Type=RelativeToParent,Property=Width,Factor=.0000,Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
            Property=Width,Factor=.3333,Constant=0}"/>
    <Button Text="Button 222" RelativeLayout.XConstraint="{ConstraintExpression
            Type=RelativeToParent,Property=Width,Factor=.3333,Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
            Property=Width,Factor=.3333,Constant=0}"/>
    <Button Text="Button 333333" RelativeLayout.XConstraint="{ConstraintExpression
            Type=RelativeToParent,Property=Width,Factor=.6666,Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
            Property=Width,Factor=.3333,Constant=0}"/>
</RelativeLayout>

enter image description here

StackLayout:

<StackLayout Orientation="Horizontal">
    <Button x:Name="button1" Text="Button 1"/>
    <Button Text="Button 222" WidthRequest="{Binding Path=Width, Source={x:Reference button1}"/>
    <Button Text="Button 333333" WidthRequest="{Binding Path=Width, Source={x:Reference button1}"/>
</StackLayout>

enter image description here