如何删除工具栏中的左侧空间以及上方和下方

问题描述

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

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

enter image description here

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

<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' 在命名空间 'Android.Support' 中不存在:

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

你是如何做到这一点的?或者你为什么没有同样的问题?

编辑:感谢 Cherry Bu - MSFT 解决方案,我可以删除左边的空间。但是上面和下面的两行仍然存在。

enter image description here

是否可能是由于 Xamarin 中的 BAD 高度计算? 这是我的工具栏的定义:

    <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 的高度???

解决方法

您可以尝试用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"

/>

并将图像或堆栈布局的 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>

enter image description here

,

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

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

PS 我假设你管理页面的方式和我一样!

,

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

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

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

MainPage = new CustomNavigationPage(new MyPage());

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

使用自定义导航页面:

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

如果您看到其他解决方案,请告诉我。如果你没有遇到这个问题,请告诉我为什么...... ;-)