ScrollView 栏自定义渲染器颜色
ScrollView bar Custom Renderer Color
正在尝试更改 ScrollView
栏的颜色。我在这里找到了这个 link,但我更愿意编写一个自定义渲染器,就像我对所有其他控件所做的那样:
问题是,我找不到任何对条形颜色有任何影响的 property/method。到目前为止,这是我的尝试:
public class CustomScrollRenderer : ScrollViewRenderer
{
public CustomScrollRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
this.ScrollBarSize = 50;
this.ScrollBarDefaultDelayBeforeFade = 60000;
this.SetBackgroundColor(Android.Graphics.Color.Red);
this.SetOutlineAmbientShadowColor(Android.Graphics.Color.Red);
this.SetOutlineSpotShadowColor(Android.Graphics.Color.Red);
}
}
在iOS中,需要创建UIScrollView的子类,重写方法LayoutSubviews
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
using App1;
using App1.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using ObjCRuntime;
[assembly:ExportRenderer(typeof(ScrollView),typeof(MyScrollViewRenderer))]
namespace App1.iOS
{
public class MyScrollViewRenderer: ViewRenderer<ScrollView, UIScrollView>
{
protected override void OnElementChanged(ElementChangedEventArgs<ScrollView> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
SetNativeControl(new MyScrollView());
}
}
}
public class MyScrollView : UIScrollView
{
public override void LayoutSubviews()
{
foreach (UIView view in Subviews)
{
if (view.IsKindOfClass(new Class("UIImageView")))
{
view.BackgroundColor = UIColor.Red;
}
}
base.LayoutSubviews();
}
}
}
在Android
在 Resource -> drawable
中创建 scrollbar_style
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="45"
android:centerColor="@color/blue"
android:endColor="@color/blue"
android:startColor="@color/blue" />
<corners android:radius="8dp" />
</shape>
using Android.App;
using Android.Content;
using Android.Graphics.Drawables;
using Android.Graphics.Drawables.Shapes;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App1.Droid;
using Java.Lang.Reflect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using static Android.Icu.Text.DateFormat;
using Field = Java.Lang.Reflect.Field;
[assembly: ExportRenderer(typeof(Xamarin.Forms.ScrollView), typeof(MyScrollViewRenderer))]
namespace App1.Droid
{
class MyScrollViewRenderer : ScrollViewRenderer
{
public MyScrollViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
Field mScrollCacheField = Java.Lang.Class.FromType(typeof(Android.Views.View)).GetDeclaredField("mScrollCache");
mScrollCacheField.Accessible = true;
Java.Lang.Object mScrollCache = mScrollCacheField.Get(this); // scr is your Scroll View
Field scrollBarField = mScrollCache.Class.GetDeclaredField("scrollBar");
scrollBarField.Accessible = true;
Java.Lang.Object scrollBar = scrollBarField.Get((Java.Lang.Object)mScrollCache);
Method method = scrollBar.Class.GetDeclaredMethod("setVerticalThumbDrawable", Java.Lang.Class.FromType(typeof(Drawable)));
method.Accessible = true;
// Set your drawable here.
method.Invoke(scrollBar, Resources.GetDrawable(Resource.Drawable.scrollbar_style));
}
}
}
正在尝试更改 ScrollView
栏的颜色。我在这里找到了这个 link,但我更愿意编写一个自定义渲染器,就像我对所有其他控件所做的那样:
问题是,我找不到任何对条形颜色有任何影响的 property/method。到目前为止,这是我的尝试:
public class CustomScrollRenderer : ScrollViewRenderer
{
public CustomScrollRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
this.ScrollBarSize = 50;
this.ScrollBarDefaultDelayBeforeFade = 60000;
this.SetBackgroundColor(Android.Graphics.Color.Red);
this.SetOutlineAmbientShadowColor(Android.Graphics.Color.Red);
this.SetOutlineSpotShadowColor(Android.Graphics.Color.Red);
}
}
在iOS中,需要创建UIScrollView的子类,重写方法LayoutSubviews
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
using App1;
using App1.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using ObjCRuntime;
[assembly:ExportRenderer(typeof(ScrollView),typeof(MyScrollViewRenderer))]
namespace App1.iOS
{
public class MyScrollViewRenderer: ViewRenderer<ScrollView, UIScrollView>
{
protected override void OnElementChanged(ElementChangedEventArgs<ScrollView> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
SetNativeControl(new MyScrollView());
}
}
}
public class MyScrollView : UIScrollView
{
public override void LayoutSubviews()
{
foreach (UIView view in Subviews)
{
if (view.IsKindOfClass(new Class("UIImageView")))
{
view.BackgroundColor = UIColor.Red;
}
}
base.LayoutSubviews();
}
}
}
在Android
在 Resource -> drawable
中创建scrollbar_style
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="45"
android:centerColor="@color/blue"
android:endColor="@color/blue"
android:startColor="@color/blue" />
<corners android:radius="8dp" />
</shape>
using Android.App;
using Android.Content;
using Android.Graphics.Drawables;
using Android.Graphics.Drawables.Shapes;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App1.Droid;
using Java.Lang.Reflect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using static Android.Icu.Text.DateFormat;
using Field = Java.Lang.Reflect.Field;
[assembly: ExportRenderer(typeof(Xamarin.Forms.ScrollView), typeof(MyScrollViewRenderer))]
namespace App1.Droid
{
class MyScrollViewRenderer : ScrollViewRenderer
{
public MyScrollViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
Field mScrollCacheField = Java.Lang.Class.FromType(typeof(Android.Views.View)).GetDeclaredField("mScrollCache");
mScrollCacheField.Accessible = true;
Java.Lang.Object mScrollCache = mScrollCacheField.Get(this); // scr is your Scroll View
Field scrollBarField = mScrollCache.Class.GetDeclaredField("scrollBar");
scrollBarField.Accessible = true;
Java.Lang.Object scrollBar = scrollBarField.Get((Java.Lang.Object)mScrollCache);
Method method = scrollBar.Class.GetDeclaredMethod("setVerticalThumbDrawable", Java.Lang.Class.FromType(typeof(Drawable)));
method.Accessible = true;
// Set your drawable here.
method.Invoke(scrollBar, Resources.GetDrawable(Resource.Drawable.scrollbar_style));
}
}
}