protected override void OnLayout(bool changed, int l, int t, int r, int b) { AToolbar bar = _toolbar; // make sure bar stays on top of everything bar.BringToFront(); base.OnLayout(changed, l, t, r, b); int barHeight = ActionBarHeight(); if (barHeight != _lastActionBarHeight && _lastActionBarHeight > 0) { ResetToolbar(); bar = _toolbar; } _lastActionBarHeight = barHeight; bar.Measure(MeasureSpecFactory.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly), MeasureSpecFactory.MakeMeasureSpec(barHeight, MeasureSpecMode.Exactly)); int internalHeight = b - t - barHeight; int containerHeight = ToolbarVisible ? internalHeight : b - t; containerHeight -= ContainerPadding; Element.ContainerArea = new Rectangle(0, 0, Context.FromPixels(r - l), Context.FromPixels(containerHeight)); // Potential for optimization here, the exact conditions by which you don't need to do this are complex // and the cost of doing when it's not needed is moderate to low since the layout will short circuit pretty fast Element.ForceLayout(); for (var i = 0; i < ChildCount; i++) { AView child = GetChildAt(i); bool isBar = JNIEnv.IsSameObject(child.Handle, bar.Handle); if (ToolbarVisible) { if (isBar) { bar.Layout(0, 0, r - l, barHeight); } else { child.Layout(0, barHeight + ContainerPadding, r, b); } } else { if (isBar) { bar.Layout(0, -1000, r, barHeight - 1000); } else { child.Layout(0, ContainerPadding, r, b); } } } }
private void UpdateControl() { var effect = (ToolbarEffect)Element.Effects.FirstOrDefault(e => e is ToolbarEffect); if (effect != null && Container != null) { try { Context context = Container.Context; MainActivity mainActivity = (context as MainActivity); Toolbar toolbar = (Toolbar)LayoutInflater.FromContext(context).Inflate(Resource.Layout.Toolbar, null); toolbar.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent); (Container as ViewGroup).AddView(toolbar); toolbar.Layout(0, 0, 700, 332); //toolbar.BringToFront(); //mainActivity.SetSupportActionBar(toolbar); //mainActivity.SupportActionBar.Show(); for (int i = 0; i < Container.ChildCount; i++) { var foundChild = Container.GetChildAt(i); var type = foundChild.GetType(); } int height = toolbar.Height; //Android.Support.V7.App.ActionBar actionBar = mainActivity.SupportActionBar; //actionBar.SetDisplayHomeAsUpEnabled(true); //actionBar.SetDisplayShowTitleEnabled(false); //actionBar.SetHomeAsUpIndicator(Resource.Drawable.abc_ic_ab_back_material); //Container.AddView(toolbar); } catch (Exception) { throw; } } }
protected override void OnLayout(bool changed, int l, int t, int r, int b) { AToolbar bar = _toolbar; // make sure bar stays on top of everything bar.BringToFront(); base.OnLayout(changed, l, t, r, b); int barHeight = ActionBarHeight(); if (Element.IsSet(BarHeightProperty)) { barHeight = Element.OnThisPlatform().GetBarHeight(); } if (barHeight != _lastActionBarHeight && _lastActionBarHeight > 0) { ResetToolbar(); bar = _toolbar; } _lastActionBarHeight = barHeight; bar.Measure(MeasureSpecFactory.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly), MeasureSpecFactory.MakeMeasureSpec(barHeight, MeasureSpecMode.Exactly)); var barOffset = ToolbarVisible ? barHeight : 0; int containerHeight = b - t - ContainerTopPadding - barOffset - ContainerBottomPadding; PageController.ContainerArea = new Rectangle(0, 0, Context.FromPixels(r - l), Context.FromPixels(containerHeight)); // Potential for optimization here, the exact conditions by which you don't need to do this are complex // and the cost of doing when it's not needed is moderate to low since the layout will short circuit pretty fast Element.ForceLayout(); bool toolbarLayoutCompleted = false; for (var i = 0; i < ChildCount; i++) { AView child = GetChildAt(i); Page childPage = (child as PageContainer)?.Child?.Element as Page; if (childPage == null) { return; } // We need to base the layout of both the child and the bar on the presence of the NavBar on the child Page itself. // If we layout the bar based on ToolbarVisible, we get a white bar flashing at the top of the screen. // If we layout the child based on ToolbarVisible, we get a white bar flashing at the bottom of the screen. bool childHasNavBar = NavigationPage.GetHasNavigationBar(childPage); if (childHasNavBar) { bar.Layout(0, 0, r - l, barHeight); child.Layout(0, barHeight + ContainerTopPadding, r, b - ContainerBottomPadding); } else { bar.Layout(0, -1000, r, barHeight - 1000); child.Layout(0, ContainerTopPadding, r, b - ContainerBottomPadding); } toolbarLayoutCompleted = true; } // Making the layout of the toolbar dependant on having a child Page could potentially mean that the toolbar is not laid out. // We'll do one more check to make sure it isn't missed. if (!toolbarLayoutCompleted) { if (ToolbarVisible) { bar.Layout(0, 0, r - l, barHeight); } else { bar.Layout(0, -1000, r, barHeight - 1000); } } }