Search code examples
android-layoutxamarinxamarin.formstitleview

How to remove left space in toolbar (and above and under)


How can I remove this ugly blue spaces around the <NavigationPage.TitleView> in Xamarin.Forms ?

<NavigationPage.TitleView>
    <StackLayout Orientation="Horizontal" Margin="0" Spacing="0" Style="{StaticResource BkGroundToolbarStyle}">
        ...
    </StackLayout>
</NavigationPage.TitleView>

enter image description here

I already test this post: Wendy Zang - MSFT but it doesn't work for me as you see. Here is the content of the Toolbar.xml file in my Android project:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:contentInsetLeft="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetStart="0dp"
    android:contentInsetEnd="0dp"
    android:contentInsetStartWithNavigation="0dp"
    android:paddingLeft="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp"
    />

I also try this solution: Mauro Cavallin - Lemcube but I get an Error: 'V7' doesn't exist in the namespace 'Android.Support' in this line:

 var toolbar = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

How do you succeed with this ? Or why don't you have the same issue ?

EDIT: Thanks to Cherry Bu - MSFT solution, I can remove the left space. But the two lines above and under remains. enter image description here

Is it possible that it is due to BAD Height computation in Xamarin ? This is the definition of my toolbar :

    <NavigationPage.TitleView>
        <StackLayout Style="{DynamicResource ToolbarBkGrndColorStyle}"
                     Orientation="Horizontal" Margin="0" Spacing="0" Padding="0"
                     HorizontalOptions="Fill" VerticalOptions="Fill">
 ...

        </StackLayout>
    </NavigationPage.TitleView> 

If I replace the definition of the stacklayout with this one, the problem disappear (but my content is no longer visible ...):

<StackLayout Style="{DynamicResource ToolbarBkGrndColorStyle}"
             Orientation="Horizontal" Margin="0" Spacing="0" Padding="0"
             HorizontalOptions="FillAndExpand" HeightRequest="1000">

So, how should I define the height of this Stacklayout ???


Solution

  • Well, after a lot of research, here are my conclusions :

    • Solution of Cherry Bu resolves the problem of the left Space
    • But the HEIGHT of the <NavigationPage.TitleView> bar doesn't always exactly fit the size of the bar in the navigation page... I don't know why. So this solution doesn't solves the problem.

    My solution is to create a CustomNavigationPage to change (for each navigationPage) the background color of the Bar which is located, in fact, in the navigation pages:

    MainPage = new CustomNavigationPage(new MyPage());
    

    or

    public ICommand CMDAddMultiple => new Command(() => Application.Current.MainPage.Navigation.PushModalAsync(new CustomNavigationPage( new MyPage()) ));
    

    With the CustomNavigationPage :

    class CustomNavigationPage : NavigationPage
    {
        public CustomNavigationPage()
        {
            SetBackgroundsColor();
        }
    
        public CustomNavigationPage(Page root) : base(root)
        {
            SetBackgroundsColor();
        }
    
        private void SetBackgroundsColor()
        {
            // Set Background Colors & System Bar color
            this.SetAppThemeColor(NavigationPage.BarBackgroundColorProperty,
                             (Color)App.Current.Resources["LIGHTToolbarBkGrndColor"],
                             (Color)App.Current.Resources["DARKToolbarBkGrndColor"]);
            this.SetAppThemeColor(NavigationPage.BackgroundColorProperty,
                             (Color)App.Current.Resources["LIGHTWindowBkGrdColor"],
                             (Color)App.Current.Resources["DARKWindowBkGrdColor"]);
        }
    }
    

    If you see orther solution, please let me know. If you don't encounter this problem, please, let me know why ... ;-)