如何删除工具栏中的左侧空间(以及上方和下方) [英] How to remove left space in toolbar (and above and under)
问题描述
如何删除 Xamarin.Forms 中 <NavigationPage.TitleView>
周围这些难看的蓝色空间?
<StackLayout Orientation=水平"边距=0"间距=0"样式={StaticResource BkGroundToolbarStyle}">...</StackLayout></NavigationPage.TitleView>
我已经测试过这篇文章:
是否可能是由于 Xamarin 中的 BAD 高度计算?这是我的工具栏的定义:
<StackLayout Style="{DynamicResource ToolbarBkGrndColorStyle}";方向=水平"边距=0"间距=0"填充=0"HorizontalOptions="填充"VerticalOptions=填充">...</StackLayout></NavigationPage.TitleView>
如果我用这个替换 stacklayout 的定义,问题就会消失(但我的内容不再可见......):
那么,我应该如何定义这个 Stacklayout 的高度???
好吧,经过大量研究,以下是我的结论:
- Cherry Bu的解决方案解决了左边空间的问题
- 但是
的 HEIGHT栏并不总是完全适合导航页面中栏的大小......我不知道为什么.所以这个解决方案并不能解决问题.
我的解决方案是创建一个 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(Page root) : base(root){设置背景颜色();}私有无效 SetBackgroundsColor(){//设置背景颜色 &系统栏颜色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"]);}}
如果您看到其他解决方案,请告诉我.如果你没有遇到这个问题,请告诉我为什么...... ;-)
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>
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.
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 ???
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 ... ;-)
这篇关于如何删除工具栏中的左侧空间(以及上方和下方)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!