代码之家  ›  专栏  ›  技术社区  ›  OriginalBigBri

如何在Xamarin选取器中设置项目列表的样式(在Android中)

  •  4
  • OriginalBigBri  · 技术社区  · 7 年前

    我有一个Xamarin Android应用程序,它使用选择器从值列表中进行选择。我一直在更改应用程序的样式,但遇到了选择器问题。 虽然我可以设置文本颜色,但我无法设置占位符文本的颜色。

    在搜索SO以获取帮助后,我实现了一个自定义渲染器,现在文本和占位符显示在正确的文本中。然而,之前当我触摸占位符文本时,子对话框出现并显示所有项目,允许用户选择一个。现在我已经实现了自定义渲染器,子对话框只显示最上面的两个项目,用户必须在单击“确定”之前滚动它们。

    我有两个问题:

    1. 至少,如何使子对话框再次显示完整列表?
    2. 是否可以设置项目列表对话框的背景和文本颜色?

    XAML如下所示:

    <c:CustomPicker x:Name="DivisionList" Title="{x:Static prop:Resources.PickerDivision}" 
                            SelectedIndexChanged="DivisionList_SelectedIndexChanged">
        <Picker.Behaviors>
            <b:RequiredPickerValidator x:Name="DivValidator" IsValid="{Binding Path=BindingContext.IsDivisionValid, Mode=OneWayToSource, Source={x:Reference contentPage}}" />
        </Picker.Behaviors>
    </c:CustomPicker>
    

    CustomPicker类如下所示:

    namespace <myapp>.Portable.Controls
    {
        public class CustomPicker : Picker
        {
            public Color PlaceholderColour
            {
                get { return (Color)App.Current.Resources["PlaceholderTextColour"]; }
            }
    
            public Color TextColour
            {
                get { return (Color)App.Current.Resources["LabelTextColour"]; }
            }
    
            public Color BackgroundColour
            {
                get { return (Color)App.Current.Resources["PaneBackgroundColour"]; }
            }
        }
    }
    

    客户渲染器是:

    [assembly: ExportRendererAttribute(typeof(CustomPicker), typeof(CustomPickerRenderer))]
    namespace <myapp>.Droid.Controls.Renderers
    {
        public class CustomPickerRenderer : PickerRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
            {
                base.OnElementChanged(e);
    
                Control?.SetPadding(20, 20, 20, 20);
                if (e.OldElement != null || e.NewElement != null)
                {
                    var customPicker = e.NewElement as CustomPicker;
    
                    Android.Graphics.Color phCol = customPicker.PlaceholderColour.ToAndroid();
                    Android.Graphics.Color textCol = customPicker.TextColour.ToAndroid();
                    Android.Graphics.Color bgCol = customPicker.BackgroundColour.ToAndroid();
    
                    Control.SetBackgroundColor(bgCol);
                    Control.SetHintTextColor(phCol);
                    Control.SetTextColor(textCol);
                }
            }
        }
    }
    

    非常感谢!

    自定义渲染器之前的选择器弹出窗口: Picker popup before custom renderer

    自定义渲染器后的选择器弹出窗口: Picker popup after custom renderer

    3 回复  |  直到 7 年前
        1
  •  7
  •   Andy    6 年前

    似乎有2个选择器渲染器。

    沙马林。形式。站台安卓采摘机 & 沙马林。形式。站台安卓AppCompat。采摘机

    确保您使用最后一个,并且您的布局将与之前相同!!!

    我得到答案的来源: https://forums.xamarin.com/discussion/97150/how-to-write-a-custom-renderer-for-bindable-picker-to-change-its-font-attributes-family-size

    渲染器(无实际指示有2个): https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/renderers

        2
  •  3
  •   Robbit    7 年前

    您可以在上添加单击事件 Control ,然后您可以自定义一个对话框,在此对话框中,您可以自定义所需的任何内容、背景、项目文本颜色等。

    在您的 CustomPickerRenderer ,请执行以下操作:

    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
    
        Control?.SetPadding(20, 20, 20, 20);
        if (e.OldElement != null || e.NewElement != null)
        {
            var customPicker = e.NewElement as CustomPicker;
            ...
            // add click event
            Control.Click += Control_Click;
        }
    }
    

    您可以显示 Dialog ,并自定义其布局,例如,添加 ListView 在其布局中,您将获得显示完整列表的子对话框:

    private void Control_Click(object sender, EventArgs e)
    {
        Toast.MakeText(Xamarin.Forms.Forms.Context,"111111",ToastLength.Short).Show();
        dialog = new Dialog(Forms.Context);
        dialog.SetContentView(Resource.Layout.dialog);
        Android.Widget.Button b1 = (Android.Widget.Button)dialog.FindViewById(Resource.Id.button1);
        Android.Widget.Button b2 = (Android.Widget.Button)dialog.FindViewById(Resource.Id.button2);
        Android.Widget.ListView listView = (Android.Widget.ListView)dialog.FindViewById(Resource.Id.lv);
        listView.Adapter=new ArrayAdapter<String>(Forms.Context, Android.Resource.Layout.SimpleExpandableListItem1,
            new List<String>() { "111","222","333", "444", "555", "666", "777", "888", "999", });
        b1.Click += B1_Click;
        b2.Click += B2_Click;
        dialog.Show();
    }
    

    下面是 dialog 布局,可以为“项目列表”对话框设置背景和文本颜色:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
                  android:orientation="vertical">
      <TextView 
        android:text="123456"
        android:layout_width="match_parent"
          android:layout_height="wrap_content"/>
      <ListView
          android:id="@+id/lv"
          android:layout_width="wrap_content"
          android:layout_height="0dp"
          android:layout_weight="1"/>
      <LinearLayout
        android:layout_width="fill_parent"
          android:layout_height="wrap_content"
        android:orientation="horizontal">
      <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Cancel" />
    
      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Ok" />
      </LinearLayout>
    </LinearLayout>
    
        3
  •  0
  •   Kydark    3 年前

    要删除这些线并恢复到默认选取器视图,而不修改自定义渲染器,只需扩展此类

    Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer

    它应该看起来有点像这样;

    public class CustomPickerRenderer : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
    {
        [logic]
    }
    

    而不是

    public class CustomPickerRenderer : PickerRenderer
    {
        [logic]
    }
    

    默认情况下,您可以扩展 Xamarin.Forms.Platform.Android 类似乎添加了不同的选择器视图。