首页 > 解决方案 > 如何删除工具栏中的剩余空间(以及上方和下方)

问题描述

如何删除<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 的高度???

标签: android-layoutxamarinxamarin.formstitleview

解决方案


你可以尝试使用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>

在此处输入图像描述


推荐阅读