0

如何删除<NavigationPage.TitleView>Xamarin.Forms 周围的丑陋蓝色空间?

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

在此处输入图像描述

我已经测试了这篇文章:Wendy Zang - MSFT,但正如你所见,它对我不起作用。这是Toolbar.xml我的Android项目中文件的内容:

<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"
    />

我也尝试了这个解决方案:Mauro Cavallin - Lemcube但我得到一个错误:'V7' doesn't exist in the namespace 'Android.Support' in this line:

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

你如何成功?或者你为什么没有同样的问题?

编辑:感谢 Cherry Bu - MSFT 解决方案,我可以删除左边的空间。但是上面和下面的两条线仍然存在。 在此处输入图像描述

这可能是由于Xamarin 中的 BAD Height 计算导致的吗?这是我的工具栏的定义:

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

        </StackLayout>
    </NavigationPage.TitleView> 

如果我用这个替换stacklayout的定义,问题就会消失(但我的内容不再可见......):

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

那么,我应该如何定义这个 Stacklayout 的高度???

4

3 回答 3

2

你可以尝试使用androidx.appcompat.widget.Toolbar来替换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"

/>

并将 image 或 stacklayout HeightRequest 设置为足够。

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

在此处输入图像描述

于 2021-04-08T07:26:27.357 回答
0

保持 StackLayout 透明,并将以下内容添加到 ContentPage 的 c++ 代码隐藏中:

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

PS我假设您管理页面的方式与我相同!

于 2021-04-07T21:31:14.843 回答
0

好吧,经过大量研究,这是我的结论:

  • Cherry Bu的解决方案解决了左边空格的问题
  • 但是 <NavigationPage.TitleView> 栏的高度并不总是完全适合导航页面中栏的大小......我不知道为什么。所以这个解决方案不能解决问题。

我的解决方案是创建一个 CustomNavigationPage 来更改(对于每个 navigationPage)Bar 的背景颜色,它实际上位于导航页面中:

MainPage = new CustomNavigationPage(new MyPage());

或者

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

使用 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"]);
    }
}

如果您看到其他解决方案,请告诉我。如果您没有遇到此问题,请告诉我原因... ;-)

于 2021-04-10T22:00:19.687 回答