如何在Android上使用带Angular的NativeScript更改状态栏颜色或将其设置为透明?
我将NativeScript与Angular一起使用,似乎无法更改项目中的状态栏颜色或将其设置为透明(任何一种都可以)。相反,它不是完全透明的,而是半透明的黑色,因此背景在它后面滚动,但被状态栏变暗。我想将其设置为透明或更改颜色(与页面背景相同) 我所尝试的:如何在Android上使用带Angular的NativeScript更改状态栏颜色或将其设置为透明?,android,angular,nativescript,statusbar,transparent,Android,Angular,Nativescript,Statusbar,Transparent,我将NativeScript与Angular一起使用,似乎无法更改项目中的状态栏颜色或将其设置为透明(任何一种都可以)。相反,它不是完全透明的,而是半透明的黑色,因此背景在它后面滚动,但被状态栏变暗。我想将其设置为透明或更改颜色(与页面背景相同) 我所尝试的: 更改App\u Resources/Android/src/main/res/values/colors.xml中的“ns\u primarydarydark”和“ns\u primary”颜色(如果我将半透明状态设置为false,则在
- 更改App\u Resources/Android/src/main/res/values/colors.xml中的“ns\u primarydarydark”和“ns\u primary”颜色(如果我将半透明状态设置为false,则在启动屏幕上有效)
- 在App_Resources/Android/src/main/res/values/styles.xml的
中设置
,尽管在
(启动屏幕)上工作正常,但没有任何区别 - 设置
将文本颜色更改为黑色,并可在启动屏幕和主应用程序上工作true
- 在任何组件的构造函数中使用下面的代码不会改变我的应用程序中的任何内容,但它在我尝试的另一个项目中起作用(详细信息如下),将颜色设置为黑色:
非常感谢。您可以使用此库透明化您的状态栏 编译'com.jaeger.statusbarutil:library:1.4.0' 要使任何特定活动透明化,只需使用 StatusBarUtil.setTransparent(活动) 看起来像这样
将以下代码添加到您的
main.ts
,这应该可以完成此任务
import * as application from "tns-core-modules/application";
declare var android;
application.android.on(application.AndroidApplication.activityCreatedEvent, (event) => {
const activity = event.activity;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
activity.getWindow().setStatusBarColor(android.graphics.Color.TRANSPARENT);
} else {
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
const parent = activity.findViewById(android.R.id.content);
for (let i = 0; i < parent.getChildCount(); i++) {
const childView = parent.getChildAt(i);
if (childView instanceof android.view.ViewGroup) {
childView.setFitsSystemWindows(true);
childView.setClipToPadding(true);
}
}
});
import*作为“tns核心模块/应用程序”中的应用程序;
声明var-android;
application.android.on(application.AndroidApplication.activityCreatedEvent,(事件)=>{
const activity=event.activity;
if(android.os.Build.VERSION.SDK\u INT>=android.os.Build.VERSION\u code.LOLLIPOP){
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG\u DRAWS\u SYSTEM\u BAR\u BACKGROUNDS);
activity.getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_半透明_状态);
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG\u半透明\u导航);
activity.getWindow().setStatusBarColor(android.graphics.Color.TRANSPARENT);
}否则{
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG\u半透明\u状态);
}
const parent=activity.findviewbyd(android.R.id.content);
for(设i=0;i
它与库中的内容非常相似。如果你想使用库,你必须将其包含在你的应用程序grad中并访问StatusBarUtil类。非常感谢你的回答!我仍在尝试如何安装库。我会继续尝试。它非常简单,只需在你的Dependent C中添加compile'com.jaeger.StatusBarUtil:library:1.4.0即可y和sync然后执行您的任务非常感谢!我为此奋斗了这么久,终于成功了!我无法让库正常工作,可能是我做错了什么。不管怎样,您的代码成功了!我注意到的一件事是导航栏(底部)变为半透明,背景在其后面。我尝试删除“半透明”标志“但是如果我这样做,状态栏会变成白色。是否只需要更改状态栏?谢谢。您可以检查另一个答案中的图像。这就是它的外观,我不确定您是否只能使状态栏透明。将检查。删除行
activity.getWindow().addFlags。”(android.view.WindowManager.LayoutParams.FLAG_transparent_NAVIGATION);
似乎完成了我这边的工作,导航栏不再是透明的。在摩托罗拉设备安卓8.1上测试。
import * as application from "tns-core-modules/application";
declare var android;
application.android.on(application.AndroidApplication.activityCreatedEvent, (event) => {
const activity = event.activity;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
activity.getWindow().setStatusBarColor(android.graphics.Color.TRANSPARENT);
} else {
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
const parent = activity.findViewById(android.R.id.content);
for (let i = 0; i < parent.getChildCount(); i++) {
const childView = parent.getChildAt(i);
if (childView instanceof android.view.ViewGroup) {
childView.setFitsSystemWindows(true);
childView.setClipToPadding(true);
}
}
});