在不影响拇指的情况下增加滑块的高度 [英] Increase height of slider without affecting thumb in Android App
问题描述
我试图使用custom renderer
表示 Android
来增加Xamarin Forms
项目中滑块的高度.以下代码大部分使用ScaleY
起作用.
I am trying to increase height of a slider in a Xamarin Forms
project using a custom renderer
for Android
. Following code works for the most part using ScaleY
.
但是当我移动滑块时,阴影也会被缩放,并且拇指变得不可见.有没有办法增加滑块的大小而又不影响拇指和阴影的大小?
But when I move the slider the shadow also is scaled and the thumb become invisible. Is there a way to increase the size of the slider without affecting the thumb and size of the shadow?
自定义渲染器
[assembly: ExportRenderer(typeof(MySlider), typeof(MySliderRenderer))]
namespace CustomRenderer.Android
{
public class MySliderRenderer : Xamarin.Forms.Platform.Android.SliderRenderer
{
public MySliderRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
Control.ProgressDrawable.SetColorFilter(
new PorterDuffColorFilter(
Xamarin.Forms.Color.FromHex("#F50F76").ToAndroid(),
PorterDuff.Mode.SrcIn));
// Set Progress bar Thumb color
Control.Thumb.SetColorFilter(
Xamarin.Forms.Color.FromHex("#F50F76").ToAndroid(),
PorterDuff.Mode.SrcIn);
//Change height
Control.ScaleY = 10;
}
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
}
}
}
XAML
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CustomRenderer;assembly=CustomRenderer"
x:Class="CustomRenderer.MainPageXaml">
<StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<Label Text="Hello, Custom Renderer 444!" />
<local:MySlider Minimum="0" Maximum="400" Value="5" />
</StackLayout>
</ContentPage>
MySlider
public class MySlider : Xamarin.Forms.Slider
{
public MySlider()
{
}
}
推荐答案
由于我们无法在自定义渲染器中为SeekBar
编写样式,因此,有一种变通办法来提高样式的高度.
Since we cannot write style to SeekBar
in custom renderer, here is a workaround to increase the height of it.
您应该能够创建LayerDrawable
,然后可以删除代码Control.ScaleY = 10;
和类似如下的代码:
You should be able to create a LayerDrawable
, then you can remove the code Control.ScaleY = 10;
and code for example like this:
GradientDrawable p = new GradientDrawable();
p.SetCornerRadius(10);
p.SetColor(Color.Rgb(0x70, 0xb2, 0x3f));
ClipDrawable progress = new ClipDrawable(p, GravityFlags.Left, ClipDrawable.Horizontal);
GradientDrawable background = new GradientDrawable();
background.SetColor(Color.Rgb(0xe0, 0xe0, 0xe0));
background.SetCornerRadius(10);
LayerDrawable pd = new LayerDrawable(new Drawable[] { background, progress });
Control.SetProgressDrawableTiled(pd);
这篇关于在不影响拇指的情况下增加滑块的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!