0

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

Belight
  • 205
  • 2
  • 14
  • @ToolmakerSteve, thank for the comment but sorry, I don't understand your suggestion. I firstly used and tried to set its background. It works but with this ugly boubdaries. That's the question. I don't see where I use android specific toolbar. Then I searched the web and found those two answers. Maybee they aren't working on the same toolbar ... therefor the fact that my 2 attempts haven't any effects ... So, back to origin, what is the problem and how to solve it ... – Belight Apr 07 '21 at 20:51
  • I would leave the StackLayout's background transparent and instead set the colour of the navigation bar. You might also want to set the StackLayout to fill in both directions. – Robot Head Apr 07 '21 at 21:18
  • I updated my 'TitleView' TAG like this and it is even worse. It is like there is no active BackgroundColor Property on the NavigationPage.TitleView. All the background is now blue ... ` ` – Belight Apr 07 '21 at 21:28
  • Ah, sorry. You'll need to do that in the code-behind. I'll post an answer so the text isn't a mess. – Robot Head Apr 07 '21 at 21:30

4 Answers4

2

You can try to use androidx.appcompat.widget.Toolbar to replace android.support.v7.widget.Toolbar.

<androidx.appcompat.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"    
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"

/>

And set image or stacklayout HeightRequest to enough.

 <NavigationPage.TitleView>
    <StackLayout
        Margin="0"
        BackgroundColor="Gray"
        HorizontalOptions="FillAndExpand"
        Orientation="Horizontal"
        Spacing="0"
        VerticalOptions="FillAndExpand">
        <Label Text="Hello World" />
        <Image HeightRequest="80" Source="check.png" />
    </StackLayout></NavigationPage.TitleView>

enter image description here

Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16
  • Great !!! I never saw this solution as clearly than here explained ! I will add a link I the other pages I visited to this post. Thanks a lot !!! – Belight Apr 08 '21 at 08:40
  • Hello, just one more question ... The left space disappeared but I can't get rid of the two small blue bars above and under the titleView. Any idea ? – Belight Apr 08 '21 at 21:10
  • To further clarify this: You have to create a xml file within the Resources/layout folder in your Android project and add the following line in your `MainActivity.cs` before `base.OnCreate(savedInstanceState);`: `ToolbarResource = Resource.Layout.Toolbar;`. `Toolbar` corresponds to the name you gave the xml file. – Christoph Mett May 20 '21 at 15:21
0

Leave the StackLayout transparent and add the following to the c++ code-behind of your ContentPage:

protected override void OnAppearing()
{
    (this.Parent as NavigationPage).Style = (Style)Application.Current.Resources["LIGHTWindowBkGrdColor"];
    base.OnAppearing();
}

PS I assume you're managing pages the same way I am!

Robot Head
  • 396
  • 3
  • 16
  • Thanks for your idea. I tried your solution with a Red color. It doesn't change anything on the result BUT ... I remark something: The whole page firstly become Red as expected, THEN the "blue frame" comes "ontop" on the expected right color. And I can't detect which object/construction is causing this trouble. I will post a screenshot in the main question. – Belight Apr 08 '21 at 07:24
0

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

Belight
  • 205
  • 2
  • 14
0

This is what is working for me, at the moment:

<Application.Resources>
   <Style TargetType="NavigationPage">
      <Setter Property="BarBackgroundColor" Value="Gray"/>
   </Style>
</Application.Resources>
Buzz
  • 512
  • 1
  • 4
  • 11