自定义搜索栏与圆角xamarin形成android

Riy*_*yas 3 c# android custom-renderer xamarin xamarin.forms

具有圆角的自定义searchBar渲染器:iOS

 protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
            {
                base.OnElementChanged(e);

                var searchbar = (UISearchBar)Control;                
                if (e.NewElement != null)
                {                    
                    searchbar.Layer.CornerRadius = 20;
                    searchbar.Layer.BorderWidth =14;
                    searchbar.Layer.BorderColor =  UIColor.FromRGB(240,240,240).CGColor;
                }
            }
Run Code Online (Sandbox Code Playgroud)

自定义SearchBar渲染器:Android?

我需要做同样的事情,就像我为ios,自定义搜索栏与圆角和其他一些自定义,为Android我没有得到足够的信息来解决这个问题.任何人都会提供一些指示或想法.

提前致谢.

Sus*_*ver 7

在您的SearchBarRenderer子类中为Android分配SearchView一个自定义形状 drawable:

class CustomSearchBar : SearchBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
            Control.Background = ContextCompat.GetDrawable(Forms.Context, Resource.Drawable.custom_search_view);
    }
}
Run Code Online (Sandbox Code Playgroud)

在drawable中,根据您的要求进行定制:

<?xml version="1.0" encoding="UTF-8" ?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:padding="10dp"
 android:shape="rectangle" >
 <corners android:radius="10dp" /> 
 <solid android:color="#baf4ed" />
  <stroke
    android:width="5.0dp"
    android:color="#800000" />
 </shape>
Run Code Online (Sandbox Code Playgroud)

有关drawables的更多信息,请查看Android文档:

回复:https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>
Run Code Online (Sandbox Code Playgroud)


小智 5

不太确定您的具体要求,但是简单地用框架包围搜索栏怎么样?

<Frame Padding="0" Margin="0" BackgroundColor="White" HasShadow="False" BorderColor="Black" CornerRadius="15" HeightRequest="30">
   <SearchBar BackgroundColor="Transparent"/>
</Frame>
Run Code Online (Sandbox Code Playgroud)

搜索栏位于修剪后的框架图像内