如何使用 Xamarin 渐变填充系统状态栏 [英] How to Gradient Fill the System Status Bar using Xamarin

查看:28
本文介绍了如何使用 Xamarin 渐变填充系统状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Xamarin application, with a main page that looks like

这看起来不错(只是处理 Android).但我想要的是系统工具栏(显示接收、电池等的栏)与 ToolBar 具有相同的颜色和相同的渐变.所以看起来像

this looks good (just dealing with Android). But what I want is the system tool bar (the bar that shows reception, battery etc.) to be the same color and with the same gradient as the ToolBar. So something that looks like

我有以下代码用于 Android 项目中的 CustomPageRenderer

I have the following code for a CustomPageRenderer in the Android project

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

[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))] namespace Prox.Droid.Renderers { public class CustomPageRenderer : PageRenderer { public CustomPageRenderer(Context context) : base(context) { }

    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>

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

Where I have tried adding the following

<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",这只会使栏变灰.我看过这个问题和答案,但这没有帮助.

to the style "MainTheme", this just greys out the bar. I have looked at This Question and Answers but this has not helped.

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

I have also attempted to use a custom NavigationBar with the background set as follows

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

没有快乐.

问.如何让系统工具栏与工具栏融合?

Q. How can I make the system tool bar blend in with the ToolBar?

推荐答案

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

There are multiple ways of doing this!

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

The easier way is to set status bar transparent and make the toolbar that has the gradient bigger. You could check to see if the SDK version is greater than or equal to kitkat then add flags so that the layout has no limits and the navigation is translucent. Then just directly in your layout, place the back ground with your gradient and add what ever label/toolbar title you like to add.

这篇关于如何使用 Xamarin 渐变填充系统状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆