如何使用Xamarin渐变填充系统状态栏

时间:2019-06-05 14:53:24

标签: android xamarin toolbar

我有一个Xamarin应用程序,其主页看起来像

GUI

这看起来不错(仅适用于Android)。但是我想要的是系统工具栏(显示接收,电池等的栏),其颜色和渐变与ToolBar相同。看起来像

Wanted

我在Android项目中为CustomPageRenderer使用了以下代码

[程序集:ExportRenderer(typeof(ContentPage),typeof(CustomPageRenderer))] 命名空间Prox.Droid.Renderers {     公共类CustomPageRenderer:PageRenderer     {         公共CustomPageRenderer(上下文上下文):base(上下文){}

    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
        base.OnElementChanged(e);

        var toolbar = MainActivity.RootFindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        if (toolbar == null)
            return;

        // Default theme colors come from the shared project. 
        DefaultTheme defaultTheme = new DefaultTheme();
        toolbar.SetBackground(new GradientDrawable(GradientDrawable.Orientation.RightLeft,
            new int[] { defaultTheme.ThemeLightColor.ToAndroid(), defaultTheme.ThemeDarkColor.ToAndroid() }));
    }
}

}

我的styles.xml看起来像

<?xml version="1.0" encoding="utf-8" ?>
<resources>

    <style name="MainTheme" parent="MainTheme.Base"></style>

    <!-- Base theme applied no matter what API -->
    <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>

        <item name="colorPrimary">#17AEC6</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">#009FB6</item>
        <!-- colorAccent is used as the default value for colorControlActivated which is used to tint widgets -->
        <item name="colorAccent">#93CD8B</item>
        <!-- You can also set colorControlNormal, colorControlActivated colorControlHighlight and colorSwitchThumbNormal. -->
        <item name="windowActionModeOverlay">true</item>

        <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
    </style>

    <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">#17AEC6</item>
    </style>
</resources>

我尝试添加以下内容的地方

<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

更改为“ MainTheme”样式,这只会使该栏变灰。我看过This Question and Answers,但这没有帮助。

我还尝试使用自定义NavigationBar,其背景设置如下

public class CustomNavigationPage : NavigationPage
{
    public CustomNavigationPage() { }
    public CustomNavigationPage(Page root) : base(root) 
    {
        BarBackgroundColor = Color.Transparent;
        BarTextColor = Color.White;
    }
}

不高兴。

问:如何使系统工具栏与工具栏融合在一起?

1 个答案:

答案 0 :(得分:0)

有多种方法可以做到这一点!

更简单的方法是将状态栏设置为透明,并使具有渐变的工具栏更大。 您可以检查一下SDK版本是否大于或等于kitkat,然后添加标志,以使布局没有限制并且导航是半透明的。然后直接在您的布局中,使用渐变放置背景,然后添加您要添加​​的标签/工具栏标题。