【问题标题】:Stack layout wont fit elements when large text is On当大文本打开时,堆栈布局不适合元素
【发布时间】:2020-11-24 11:28:35
【问题描述】:

我正在尝试使我的应用程序与辅助功能更兼容,但当启用大文本时,我无法在水平方向的堆栈中容纳两个元素。知道如何解决这个问题吗?

        StackLayout horisontalContainer = new StackLayout
        {
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.FillAndExpand,
        };

        List<string> dataList = this.registerCountries();
        List<string> numberList = this.registerAreaCodes();
        List<string> pickerData = new List<string>();
        int i = 0;
        foreach (var data in dataList)
        {
            pickerData.Add(data + " +" + numberList[i]);
            i++;
        }

        countryCode = new Picker { Title = "Country code" };
        countryCode.ItemsSource = pickerData;
        countryCode.IsEnabled = true;
        countryCode.SelectedIndex = 0;
        countryCode.SelectedIndexChanged += (sender, args) =>
        {
            var tempValue = countryCode.Items[countryCode.SelectedIndex];
            string[] words = tempValue.Split('+');
            pickerSelection = words[words.Length-1];
        };

        phoneNbrEntry = new Entry
        {
            Placeholder = Localization.MobileNumber,
            FontSize = 18,
            BackgroundColor = Color.White.ToFormsColor(),
            TextColor = Color.Black.ToFormsColor(),
            Keyboard = Keyboard.Telephone,
        };

#if IOS phoneNbrEntry.WidthRequest = UIKit.UIScreen.MainScreen.Bounds.Width/2-20; #endif

#if 安卓 phoneNbrEntry.WidthRequest = CrossScreen.Current.Size.Width/2-20; #endif

        horisontalContainer.Children.Add(countryCode);
        horisontalContainer.Children.Add(phoneNbrEntry);

图片:

【问题讨论】:

  • 现在可以用了吗?
  • @LucasZhang-MSFT 是的。我希望有一个堆栈解决方案,但我确实将其更改为网格

标签: c# xaml xamarin xamarin.forms xamarin.ios


【解决方案1】:

StackLayout 的大小与其子元素不匹配。在您的情况下,最好使用 Grid 而不是 Stacklayout 以便我们可以设置 PickerLabel 作为相对值。

    Grid grid = new Grid
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.CenterAndExpand,

            Padding = new Thickness(10,0),

            RowDefinitions =
            {
              
              new RowDefinition { Height = new GridLength(60) },
               
            },

            ColumnDefinitions =
            {

              // the width of picker and label will fill half of the screen
              new ColumnDefinition{Width = new GridLength(1, GridUnitType.Star) },
              new ColumnDefinition{Width = new GridLength(1, GridUnitType.Star) },
            }
        };

        var countryCode = new Picker { Title = "Country code" };
        //countryCode.ItemsSource = pickerData;
        countryCode.IsEnabled = true;
        countryCode.TitleColor = Color.Red;
        countryCode.SelectedIndex = 0;

        var phoneNbrEntry = new Entry
        {
            Placeholder = "11111111",
            FontSize = 38,
            BackgroundColor = Color.White,
            TextColor = Color.Black,
            Keyboard = Keyboard.Telephone,
        };

        grid.Children.Add(countryCode,0,0);
        grid.Children.Add(phoneNbrEntry,1, 0);


        
        Content = grid;

    }

Effect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    相关资源
    最近更新 更多