Fal*_*lko 0 android custom-renderer searchbar xamarin xamarin.forms
我在具有深色背景颜色的StackLayout上有一个Xamarin.Forms SearchBar。
默认情况下,输入字段也是暗的,可能是由于背景透明。要修改搜索栏的背景色,有一个BackgroundColor属性。(在下面的示例中,我将其设置为深灰色。)
但是如何调整文字颜色?没有这样的属性。即使使用自定义搜索栏渲染器,我也找不到解决方案。
仅占位符的外观如下:

...,并加上一些输入(黑色的“ Hello world!”,在深色背景上):

小智 5
这是将完成此操作的Android自定义渲染器的代码。我会上传一个屏幕截图,但我的积分还不够:(
using System;
using Android.Widget;
using Android.Text;
using G = Android.Graphics;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly:ExportRenderer( typeof(MySearchBar), typeof(Some.Namespance.MySearchBar_Droid) )]
namespace Some.Namespace
{
public class MySearchBar_Droid : SearchBarRenderer
{
protected override void OnElementChanged( ElementChangedEventArgs<SearchBar> args )
{
base.OnElementChanged( args );
// Get native control (background set in shared code, but can use SetBackgroundColor here)
SearchView searchView = (base.Control as SearchView);
searchView.SetInputType( InputTypes.ClassText | InputTypes.TextVariationNormal );
// Access search textview within control
int textViewId = searchView.Context.Resources.GetIdentifier( "android:id/search_src_text", null, null );
EditText textView = (searchView.FindViewById( textViewId ) as EditText);
// Set custom colors
textView.SetBackgroundColor( G.Color.Rgb( 225, 225, 225 ) );
textView.SetTextColor( G.Color.Rgb( 32, 32, 32 ) );
textView.SetHintTextColor( G.Color.Rgb( 128, 128, 128 ) );
// Customize frame color
int frameId = searchView.Context.Resources.GetIdentifier( "android:id/search_plate", null, null );
Android.Views.View frameView = (searchView.FindViewById( frameId ) as Android.Views.View);
frameView.SetBackgroundColor( G.Color.Rgb( 96, 96, 96 ) );
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4574 次 |
| 最近记录: |