【问题标题】:How can I achieve four spaced buttons on an iPhone that fill the screen and on bigger than an iPhone that fill half the screen?如何在填充屏幕的 iPhone 上实现四个间隔按钮,并且在比填充一半屏幕的 iPhone 上更大?
【发布时间】:2018-01-08 22:10:02
【问题描述】:

我想要四个这样的按钮:

<Grid x:Name="buttonGrid" Grid.Row="3" Grid.Column="0" HorizontalOptions="FillAndExpand" VerticalOptions="Center" Padding="20, 0">
    <Button x:Name="zeroButton" Text="0" HeightRequest="60" BackgroundColor="#2D7BF7" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand"/>
    <Button x:Name="oneButton" Text="1" HeightRequest="60" BackgroundColor="#2D7BF7" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand"/>
    <Button x:Name="twoButton" Text="2" HeightRequest="60" BackgroundColor="#2D7BF7" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand"/>
    <Button x:Name="fiveButton" Text="5" HeightRequest="60" BackgroundColor="#2D7BF7" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand"/>
</Grid>

在 iPhone 或小型移动设备上,我希望这些填充 90% 的屏幕宽度。但在更大的屏幕上,我希望按钮只填充屏幕宽度的 50%。

谁能建议我如何做到这一点?

【问题讨论】:

  • 使用 OnIdiom 为手机和平板电脑指定不同的布局
  • 很抱歉,我对此一无所知。这是很多人用的吗?
  • 是的,XF 专门针对这种情况提供 OnIdiom:您希望在手机和平​​板电脑上有不同的行为。
  • Jason - 你能举个例子吗?顺便说一句,我可能会在接下来的 24 小时内为这个问题提供悬赏 :-)
  • 您是否尝试过搜索?有很多例子,比如:stackoverflow.com/questions/40111252/…。您还可以为每个惯用语创建单独的 XAML 布局,然后选择以编程方式调用哪个布局。

标签: c# xamarin xamarin.forms


【解决方案1】:

说明

如果您需要创建引用ContentPage.WidthContentPage.Height 属性的布局,最好的方法是使用RelativeLayout

如果您尝试直接引用ContentPage.WidthContentPage.Height 属性,您会发现Xamarin.Forms 返回-1。这是因为-1 是 Xamarin.Forms 在尚未实例化控件时使用的默认值。

RelativeLayout 使用Funcs 动态引用页面的HeightWidth 属性,并在ContentPage 实例化后返回HeightWidth 属性的真实值。

代码

using Xamarin.Forms;

namespace HorizontalButtonSampleApp
{
    public class ButtonPage : ContentPage
    {
        public ButtonPage()
        {
            const int relativeLayoutHorizontalSpacing = 5;
            const int numberOfButtons = 4;

            double screenUsePercentage;
            if (Device.Idiom.Equals(TargetIdiom.Phone))
                screenUsePercentage = 0.9;
            else
                screenUsePercentage = 0.5;

            var zeroButton = new StyledButton { Text = "0" };
            var oneButton = new StyledButton { Text = "1" };
            var twoButton = new StyledButton { Text = "2" };
            var fiveButton = new StyledButton { Text = "5" };

            var relativeLayout = new RelativeLayout();
            relativeLayout.Children.Add(zeroButton,
                                        Constraint.RelativeToParent(parent => parent.Width * (1 - screenUsePercentage) / 2),
                                        Constraint.Constant(0),
                                        Constraint.RelativeToParent(parent => (parent.Width * screenUsePercentage - relativeLayoutHorizontalSpacing * (numberOfButtons - 1)) / numberOfButtons));
            relativeLayout.Children.Add(oneButton,
                                        Constraint.RelativeToView(zeroButton, (parent, view) => view.X + view.Width + relativeLayoutHorizontalSpacing),
                                        Constraint.Constant(0),
                                        Constraint.RelativeToParent(parent => (parent.Width * screenUsePercentage - relativeLayoutHorizontalSpacing * (numberOfButtons - 1)) / numberOfButtons));
            relativeLayout.Children.Add(twoButton,
                                        Constraint.RelativeToView(oneButton, (parent, view) => view.X + view.Width + relativeLayoutHorizontalSpacing),
                                        Constraint.Constant(0),
                                        Constraint.RelativeToParent(parent => (parent.Width * screenUsePercentage - relativeLayoutHorizontalSpacing * (numberOfButtons - 1)) / numberOfButtons));
            relativeLayout.Children.Add(fiveButton,
                                        Constraint.RelativeToView(twoButton, (parent, view) => view.X + view.Width + relativeLayoutHorizontalSpacing),
                                        Constraint.Constant(0),
                                        Constraint.RelativeToParent(parent => (parent.Width * screenUsePercentage - relativeLayoutHorizontalSpacing * (numberOfButtons - 1)) / numberOfButtons));

            Content = relativeLayout;

            Padding = new Thickness(0, 20);

            Title = $"Button Page on a {Device.Idiom}";
        }
    }

    public class App : Application
    {
        public App()
        {
            MainPage = new NavigationPage(new ButtonPage());
        }
    }

    public class StyledButton : Button
    {
        public StyledButton()
        {
            TextColor = Color.Black;
            HeightRequest = 60;
            BackgroundColor = Color.FromHex("2D7BF7");
            HorizontalOptions = LayoutOptions.FillAndExpand;
            VerticalOptions = LayoutOptions.CenterAndExpand;
        }
    }
}

平板电脑屏幕截图

手机屏幕截图

【讨论】:

  • 嗨布兰登,我很抱歉,但我需要的是它们水平间隔。对困惑感到抱歉。如果您想为此修改答案,那么我可以接受。
  • 没问题,@Alan!我编辑了上面的代码,使按钮水平间隔!我还创建了一个名为StyledButton 的新类,它继承自Button。因为每个按钮都有相似的属性,所以我们可以在StyledButton 中设置一次相似的属性并重用它的代码!
  • 很好的答案,鉴于 Xamarin 不推荐相对布局(尽管不幸的是我个人很喜欢它),我还建议检查 AbsoluteLayout。
  • 嗨,布兰登,我的页面已经设置了 StackLayout,其中包含网格。你觉得我可以通过页面其他部分的相对布局来实现同样的效果,然后将你的按钮代码添加到它的底部吗?
  • @Alan 如果您指定已有的内容以及要将这些按钮插入的位置,则更容易为您提供更详细的答案。如果你想将它们插入到网格中,那么整个网格的大小应该改变。
猜你喜欢
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 2022-01-01
相关资源
最近更新 更多