如何减小 xamarin 表单中占位符的字体大小?

How to reduce the font size of placeholder in xamarin forms?

我有一个 UI,其中占位符文本适合搜索栏。这是我需要的UI。

这是我的UI

这是我的自定义渲染器代码

[assembly: ExportRenderer(typeof(searchTab), typeof(StyledSearchBarRenderer))]
namespace RestaurantApp.Droid.Renderers
{
#pragma warning disable CS0618 // Type or member is obsolete
    class StyledSearchBarRenderer : SearchBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                var color = global::Xamarin.Forms.Color.LightGray;

                var searchView = Control as SearchView;

                int searchPlateId = searchView.Context.Resources.GetIdentifier("android:id/search_plate", null, null);
                Android.Views.View searchPlateView = searchView.FindViewById(searchPlateId);
                searchPlateView.SetBackgroundColor(Android.Graphics.Color.Transparent);

                int searchIconId = Context.Resources.GetIdentifier("android:id/search_mag_icon", null, null);
                ImageView searchViewIcon = (ImageView)searchView.FindViewById<ImageView>(searchIconId);
                searchViewIcon.SetImageDrawable(null);

            }
        }
    }
#pragma warning restore CS0618 // Type or member is obsolete
}

这是我的XAML代码

 <Frame CornerRadius="10" Padding="0" OutlineColor="DarkGray" HasShadow="True" HorizontalOptions="Fill"  Margin="10,0,10,0" VerticalOptions="Center">
                <local:searchTab x:Name="searchBar"  Placeholder="Please search for a vendor or product name" PlaceholderColor="Black" TextColor="Black" HorizontalOptions="FillAndExpand" VerticalOptions="Center" />

            </Frame>

我不知道如何解决这个问题。有什么建议吗?

首先要了解的是,您的用户的屏幕尺寸会因他们使用的设备而异。将字体大小设置为绝对值会以指数方式使它变得过于复杂。

Apple's HIG for the SearchBar 的指导方针简短明了,应该可以帮助您实现 UI,这将使您的应用受益(即使它仅针对 Android)。在您的示例中,您希望显示文本 Please search for a product vendor or name。这是每个 UI 指南中的一个 说明 。搜索栏占位符需要是 hint,例如 SearchProduct供应商产品或供应商名称(不胜枚举)。

您的文本应作为标签显示在搜索栏上方,以便用户知道这是他们必须遵循的说明。然后在搜索栏中,您将为搜索栏显示一个简短的占位符(上面的示例)。事实上,您可以通过在标签中添加说明来改进 UI,但使用示例搜索字符串作为占位符(示例:Apple iPhone)这样用户就有了写什么的例子。

或者,您可以删除 "please type in ...",只将搜索栏占位符设置为 Product/Vendor 名称。用户仍会知道按产品或供应商名称搜索。

执行上述操作将避免创建不必要的复杂自定义渲染器。

要更改 SearchViewHintText,我们需要找到 SearchViewEditText

var textView = (searchView.FindViewById(textViewId) as EditText);

现在在获得 SearchViewEditText 实例后,我们可以设置 textView 我们可以设置所有类型的属性,例如 SetBackgroundColorSetHintTextColor、仅举几例。

因此您要更改 HintText(占位符)的代码将如下所示

var textView = (searchView.FindViewById(textViewId) as EditText);
textView.SetTextSize(12);

注意:当我们更改 TextSize 提示大小时,也会发生变化。