【问题标题】:Increase height of slider without affecting thumb in Android App增加滑块的高度而不影响Android App中的拇指
【发布时间】:2017-08-09 23:44:06
【问题描述】:

我正在尝试使用custom rendererAndroid 增加Xamarin Forms 项目中滑块的高度。以下代码大部分使用ScaleY

但是当我移动滑块时,阴影也被缩放并且拇指变得不可见。有没有办法在不影响拇指和阴影大小的情况下增加滑块的大小?

自定义渲染器

[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>

我的滑块

public class MySlider : Xamarin.Forms.Slider
{
    public MySlider()
    {

    }
}

【问题讨论】:

    标签: xamarin xamarin.android xamarin.forms


    【解决方案1】:

    由于我们无法在自定义渲染器中将样式写入SeekBar,因此这里有一个解决方法来增加它的高度。

    你应该可以创建一个LayerDrawable,然后你可以删除代码Control.ScaleY = 10;,例如这样的代码:

    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); 
    

    【讨论】:

    • 太棒了。谢谢。你有 iOS 等效的 iOS 渲染器吗?
    • @Lijo,对不起,我暂时对iOS不熟悉,Slider在iOS平台的原生控件是UISlider,有一些关于增加高度的讨论它在 SO 中,例如 this,但我无法测试 Xamarin.Forms 的解决方案,也许你可以试一试。
    猜你喜欢
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多