Xamarin.Forms中Picker的高度

时间:2015-10-07 13:50:31

标签: android xamarin xamarin.forms picker

我正在使用xamarin.forms并在屏幕上显示Bindable选择器。

我正在使用以下代码更新Picker控件的初始文本。

        public class MyCustomBindablePickerRenderer :PickerRenderer
        {
            // Override the OnElementChanged method so we can tweak this renderer post-initial setup
            protected override void OnElementChanged (ElementChangedEventArgs<Picker> e)
            {
                base.OnElementChanged (e);

                //Background = A Android.Graphics.Color.Rgb (0, 0, 0);
                if (Control != null) { 
                    // do whatever you want to the textField here!

                    Control.SetBackgroundColor(global::Android.Graphics.Color.Rgb(241,241,241));
                    //Control.ShadowRadius = 2;
                    //Control.SetCursorVisible = true;


                    var label = (TextView)Control; // for example
                    Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "avenir-book.ttf");
                    label.Typeface = font;
                    label.TextSize = CustomFonts.EntryFontSize;
                    label.SetHintTextColor (global::Android.Graphics.Color.Rgb(162,162,162));
                    label.SetTextColor (global::Android.Graphics.Color.Rgb(162,162,162));


                }
            }
        }

现在,我希望增加单击文本时显示的弹出窗口的大小。

我用google搜索并找到了事件OnWindowFocusChanged,但弹出窗口后事件会触发,所以我无法访问弹出窗口的高度。

当我点击选择器控件的文本框时,是否有人会建议事件触发?

如何更改弹出窗口的高度?

1 个答案:

答案 0 :(得分:2)

您需要创建SpinnerAdapter,以便自定义每个单元格。覆盖GetDropDownView方法,如下所示:

[assembly: ExportRenderer(typeof(UIPicker), typeof(UIPickerRenderer))]
namespace SampleApp.Android
{
    public class UIPickerRenderer: ViewRenderer<UIPicker, Spinner>
    {
        bool initialized = false;
        Spinner picker;
        static int selectedIndex=-1;
        protected override void OnElementChanged (ElementChangedEventArgs<UIPicker> e)
        {

            base.OnElementChanged (e);
            if(e.OldElement == null)
            {
                Element.SelectedIndexChanged += NotifyVM;

                selectedIndex = Element.SelectedIndex;
                this.picker = new Spinner(Forms.Context, SpinnerMode.Dropdown);
                var adapter = new SpinnerAdapter(Forms.Context, Android.Resource.Layout.SimpleSpinnerItem,Element.Items);

                picker.Adapter = adapter;
                picker.SetSelection(selectedIndex);
                picker.ItemSelected += picker_ItemSelected;
                this.SetNativeControl(picker);
                Element.SelectedIndexChanged += (object s, EventArgs args) => { 
                    if (picker.SelectedItemPosition != Element.SelectedIndex) 
                        picker.SetSelection(Element.SelectedIndex); 
                };
            }
        }

        private void picker_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            if(! initialized)
            {
                initialized = true;
                return;
            }
            Element.SelectedIndex = picker.SelectedItemPosition;
            var g = picker.SelectedItemPosition;
        }

        private async void NotifyVM(object sender, EventArgs e)
        {

            if (selectedIndex == Element.SelectedIndex)
                return;
            selectedIndex = Element.SelectedIndex;


            if (Element.ParentView.BindingContext != null && Element.IsVisible && Element.ShouldUpdateServices)
            {
               //Do your binding things
            }
        }

        internal class SpinnerAdapter : ArrayAdapter<string>
        {
            public SpinnerAdapter(Android.Content.Context context, int id, System.Collections.Generic.IList<string> items)
                : base(context, id, items)
            {
            }

            public override Android.Views.View GetDropDownView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
            {
                convertView = base.GetDropDownView(position, convertView, parent);

                convertView.Visibility = Android.Views.ViewStates.Visible;
                FormsViewGroup.LayoutParams p = convertView.LayoutParameters;
                p.Height = 60; //adjusting cell height
                p.Width = 300; //Adjusting cell width
                convertView.LayoutParameters = p;
                return convertView;
            }
        }

    }
}