恢复观看的相对位置

时间:2018-02-14 09:04:10

标签: c# layout view xamarin.android rangeslider

编辑:我忘了描述我的实际探测:D在方向改变后,肖像 - >横向,反之亦然,拇指布局不正确。我希望布局中间(宽度/ 2)的拇指在方向改变后保持在布局的中间位置。

我正在尝试创建一个“范围滑块”。 我所拥有的是两个简单的视图,代表两个拇指和一个代表滑块的框架布局。

基于滑块的宽度乘以拇指的值(介于0和1之间),我在重写的OnLayout方法中布置了我的两个拇指。

public class RangeSliderView : ViewGroup
{

public RangeSliderView(Context context) : base(context)
{
    var thumbL = new Thumb(context);
    var thumbR = new Thumb(context);
    AddView(thumbL);
    AddView(thumbR);
}

protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
    if (changed)
    {
        //Get the width and height if not set yet
        if (ThumbLeft.MeasuredWidth == 0 || ThumbLeft.MeasuredHeight == 0)
            ThumbLeft.Measure(MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified), MeasureSpec.MakeMeasureSpec(b, MeasureSpecMode.AtMost));

        if (ThumbRight.MeasuredWidth == 0 || ThumbRight.MeasuredHeight == 0)
            ThumbRight.Measure(MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified), MeasureSpec.MakeMeasureSpec(b, MeasureSpecMode.AtMost));

        //calculate width and the relative position of the thumbs
        int width = r - l;
        int thumbLeftX = (int)(width * lastProgressLeft); //last progresses are a value between 0 and 1
        int thumbRightX = (int)(width * lastProgressRight);

        //position the thumbs
        ThumbLeft.Layout(l: l + thumbLeftX,
                         t: t,
                         r: l + thumbLeftX + ThumbLeft.MeasuredWidth,
                         b: ThumbLeft.MeasuredHeight);
        ThumbRight.Layout(l: l + thumbRightX - ThumbRight.MeasuredWidth,
                          t: t,
                          r: l + thumbRightX,
                          b: ThumbLeft.MeasuredHeight);
    }
}
}

值似乎是正确的,在景观中,“r”值大于纵向。 “l”始终为0,因为控件与屏幕左侧对齐。计算似乎是正确的,我测试了将其中一个拇指移到中间,所以我可以准确地看到thumbLeftX或thumbRightX是否是宽度的50%。这似乎是正确的。

我认为拇指上的布局(...)调用不能可靠地布置我的拇指。 是否有我错过的布局调用? 我是否需要调用其他方法来正确地重新布置我的拇指?

最初,lastProgressLeft为0,lastProgressRight为1(意味着拇指左侧应位于滑块的左端,右侧拇指位于滑块的右端。这很有效,也适用于方向更改,它看起来很棒正确的。

0 个答案:

没有答案
相关问题