UnityTech / UIWidgets

UIWidget is a Unity Package which helps developers to create, debug and deploy efficient, cross-platform Apps.
Other
1.97k stars 256 forks source link

AppBar如何适配全面屏沉浸式状态栏颜色 #119

Closed jiangheng90 closed 5 years ago

jiangheng90 commented 5 years ago

在UIWidgets中是否有直接方法设置? 我现在的做法是做一个方法设置状态栏

---设置状态栏方法

using UnityEngine;

public class StatusBar : MonoBehaviour
{
    private static int newStatusBarValue = 1024;
    /// <summary>
    ///  隐藏上方状态栏
    /// </summary>
    public static void Hide()
    {
        #if !UNITY_EDITOR && UNITY_ANDROID
                setStatusBarValue(1024); // WindowManager.LayoutParams.FLAG_FULLSCREEN; change this to 0 if unsatisfied
        #endif
    }

    /// <summary>
    ///  显示上方状态栏
    /// </summary>
    public static void Show()
    {
        #if !UNITY_EDITOR && UNITY_ANDROID
                setStatusBarValue(2048); // WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
        #endif
    }

    private static void setStatusBarValue(int value)
    {
        newStatusBarValue = value;
        Color statusBarColor = Color.blue;
        using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
            {
                activity.Call("runOnUiThread", new AndroidJavaRunnable(setStatusBarValueInThread));
            }
        }
    }

    private static void setStatusBarValueInThread()
    {
        using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        {
            using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
            {
                using (var window = activity.Call<AndroidJavaObject>("getWindow"))
                {
                    window.Call("setFlags", newStatusBarValue, newStatusBarValue);
                }
            }
        }
    }
}

再Widget Build之前调用

public override Widget build(BuildContext context)
            {
                StatusBar.Show();
                return new MaterialApp
                (
                    home: new Scaffold
                    (
                        appBar: new AppBar
                        (
                            leading: new SafeArea
                            (
                                child: new IconButton
                                (
                                    icon: new Icon(Icons.menu),
                                    tooltip: "Navigation menu",
                                    onPressed: null
                                )
                            ),
.......

每次进入App之后就会就会如下图 1

但是多任务后就会恢复正常 2

有什么办法能够解决这个问题?

jiangheng90 commented 5 years ago

在OnEable()之前调用可以解决该问题

protected override void OnEnable()
        {
            StatusBar.Show();
            base.OnEnable();
            FontManager.instance.addFont(Resources.Load<Font>(path: "MaterialIcons-Regular"), "Material Icons");
        }