[英]completely transparent status bar and navigation bar on lollipop
I'm trying to make an android launcher.我正在尝试制作一个android启动器。 I want to achieve a completely transparent status bar and navigation bar, here is my theme xml file.我想实现一个完全透明的状态栏和导航栏,这里是我的主题xml文件。
<resources>
<style name="Theme" parent="android:Theme.Material.Wallpaper.NoTitleBar">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>
</resources>
You can achieve the same effect programmatically on KitKat and afterward by setting the FLAG_LAYOUT_NO_LIMITS
flag inside the Window
.您可以通过在Window
内设置FLAG_LAYOUT_NO_LIMITS
标志在 KitKat 和之后以编程方式实现相同的效果。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow(); // in Activity's onCreate() for instance
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
If you set a background resource ( like a color or a picture ) to your layout, you will see the color or picture "below" the status bar.如果您为布局设置背景资源(如颜色或图片),您将在状态栏“下方”看到颜色或图片。
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/primary_dark</item>
It looks like android:windowTranslucentStatus
and android:windowTranslucentNavigation
should be true
instead of false
看起来android:windowTranslucentStatus
和android:windowTranslucentNavigation
应该是true
而不是false
<resources>
<style name="Theme" parent="android:Theme.Material.Wallpaper.NoTitleBar">
<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>
</style>
</resources>
Also, your transparent activity / container layout needs this property set:此外,您的透明活动/容器布局需要此属性集:
android:fitsSystemWindows="true"
[Source][1] [1]: https://stackoverflow.com/a/29311321/1549700 [来源][1] [1]:https://stackoverflow.com/a/29311321/1549700
I use this since it keeps the height of the status bar and nav bar我使用它是因为它保持状态栏和导航栏的高度
<!-- Base application theme. -->
<style name="theme" parent="android:Theme.Material.Wallpaper.NoTitleBar">
<item name="android:navigationBarColor">#00000000</item>
<item name="android:statusBarColor">#00000000</item>
</style>
This does require API 21+ however但是,这确实需要 API 21+
You can use this kotlin extension function it will set status bar fully transparent (on API 23+ , View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
flag available on API 23+ ) and navigation bar (on API 27+ , View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
flag available on API 27+ ) otherwise it will use the systemUiScrim
color on API 21+你可以使用这个 kotlin 扩展函数,它会设置状态栏完全透明(在API 23+ 上, View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
标志在API 23+上可用)和导航栏(在API 27+ 上, View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
标志在API 27+上可用)它将在API 21+上使用systemUiScrim
颜色
fun Activity.transparentStatusAndNavigation(
systemUiScrim: Int = Color.parseColor("#40000000") // 25% black
) {
var systemUiVisibility = 0
// Use a dark scrim by default since light status is API 23+
var statusBarColor = systemUiScrim
// Use a dark scrim by default since light nav bar is API 27+
var navigationBarColor = systemUiScrim
val winParams = window.attributes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
systemUiVisibility = systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
statusBarColor = Color.TRANSPARENT
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
systemUiVisibility = systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
navigationBarColor = Color.TRANSPARENT
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
systemUiVisibility = systemUiVisibility or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
window.decorView.systemUiVisibility = systemUiVisibility
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
winParams.flags = winParams.flags or
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
winParams.flags = winParams.flags and
(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION).inv()
window.statusBarColor = statusBarColor
window.navigationBarColor = navigationBarColor
}
window.attributes = winParams
}
For API 29 and above use对于 API 29 及以上使用
<style name="Your.Theme">
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:enforceNavigationBarContrast">false</item>
</style>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initStatusNavBar();
transparentStatusAndNavigation();
showSystemUI();
// hideSystemUI();
}
public static void transparentStatusAndNavigation(Activity activity) {
Window window = activity.getWindow();
// make full transparent statusBar
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
setWindowFlag(window, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, true);
}
if (Build.VERSION.SDK_INT >= 19) {
int visibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
visibility = visibility | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
window.getDecorView().setSystemUiVisibility(visibility);
}
if (Build.VERSION.SDK_INT >= 21) {
int windowManager = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
windowManager = windowManager | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
setWindowFlag(window, windowManager, false);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
}
private static void setWindowFlag(final int bits, boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
in activity_main.xml:在activity_main.xml 中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">
<View
android:id="@+id/status_bg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/primaryColorLightThemeDarkTrans"
tools:layout_height="24dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--Write Code Here-->
</LinearLayout>
<View
android:id="@+id/nav_bg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/navColorLightThemeTrans"
tools:layout_height="?actionBarSize" />
</LinearLayout>
in java code:在java代码中:
private void initStatusNavBar() {
int statusBarHeight = getStatusBarHeight(activity);
int navBarHeight = getNavigationBarHeight(activity, statusBarHeight);
View statusBarBackground = findViewById(R.id.status_bg);
statusBarBackground.getLayoutParams().height = statusBarHeight;
View navBarBackground = findViewById(R.id.nav_bg);
if (Build.VERSION.SDK_INT >= 21) {
setNavigationBarHeight(activity, navBarBackground);
} else {
navBarBackground.getLayoutParams().height = navBarHeight;
}
}
public static int getStatusBarHeight(Activity activity) {
final Resources resources = activity.getResources();
final int resId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
return resources.getDimensionPixelSize(resId);
}
return 0;
}
public static int getNavigationBarHeight(Activity activity, int statusBarHeight) {
Point point = getNavigationBarSize(activity);
int height = point.y;
if (isNotchDisplay(statusBarHeight)) {
height = height - statusBarHeight;
}
return height;
}
private static Point getNavigationBarSize(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Point appUsableSize = getAppUsableScreenSize(context);
Point realScreenSize = getRealScreenSize(context);
// navigation bar on the right
if (appUsableSize.x < realScreenSize.x) {
return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
}
// navigation bar at the bottom
if (appUsableSize.y < realScreenSize.y) {
return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
}
// navigation bar is not present
return new Point();
}
return new Point();
}
private static Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
if (null != windowManager) {
Display display = windowManager.getDefaultDisplay();
display.getSize(size);
}
return size;
}
private static Point getRealScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
if (null != windowManager) {
Display display = windowManager.getDefaultDisplay();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else {
try {
size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
return size;
}
private static boolean isNotchDisplay(int statusBarHeight) {
int normalStatusBarHeight = dpToPxForNav(25);
return statusBarHeight > normalStatusBarHeight;
}
private static int dpToPxForNav(float dp) {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);
}
public static void setNavigationBarHeight(Activity activity, View navBarBackground) {
ViewCompat.setOnApplyWindowInsetsListener(navBarBackground, (v, insets) -> {
int navBarHeight = insets.getSystemWindowInsetBottom();
navBarBackground.getLayoutParams().height = navBarHeight;
return insets.consumeSystemWindowInsets();
});
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void showSystemUI() {
statusBarBackground.setVisibility(View.VISIBLE);
navBarBackground.setVisibility(View.VISIBLE);
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void hideSystemUI() {
statusBarBackground.setVisibility(View.GONE);
navBarBackground.setVisibility(View.GONE);
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
<style name="MyAppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<!--For Notch Issues-->
<item name="android:windowLayoutInDisplayCutoutMode" tools:targetApi="o_mr1">shortEdges</item>
<item name="windowActionBarOverlay">true</item>
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryColorDark</item>
<item name="colorAccent">@color/accentColor</item>
<item name="colorControlHighlight">@color/colorHighlight</item>
<item name="android:windowBackground">@android:color/white</item>
<item name="android:textColorPrimary">#fff6d7</item>
<item name="android:colorPrimary" tools:targetApi="lollipop">@color/primaryColor</item>
<item name="android:colorPrimaryDark" tools:targetApi="lollipop">@color/primaryColorDark</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/primaryColorDark</item>
<item name="android:colorAccent" tools:targetApi="lollipop">@color/accentColorLight</item>
<item name="android:colorControlHighlight" tools:targetApi="lollipop">@color/colorHighlight</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">@color/navColor</item>
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>
</style>
<activity
android:name="com.MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="fullSensor"
android:theme="@style/MyAppTheme" />
You need to add android:windowDrawsSystemBarBackgrounds
flag to you theme您需要为您的主题添加android:windowDrawsSystemBarBackgrounds
标志
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
Or call this in onCreate()或者在 onCreate() 中调用它
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
The following code is an example of what I use in my project:以下代码是我在项目中使用的示例:
styles.xml样式文件
<style name="FadingActionBarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/FadingActionBarWidget</item>
</style>
<style name="FadingActionBarWidget.Transparent">
<item name="android:background">@android:color/transparent</item>
</style>
<style name="FadingActionBarTheme.TranslucentActionBar">
<item name="android:icon">@drawable/ic_ab_icon</item>
<item name="android:actionBarStyle">@style/FadingActionBarWidget.Transparent</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
AndroidManifest.xml AndroidManifest.xml
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@style/FadingActionBarTheme.TranslucentActionBar">
</activity>
To draw your layout under statusbar :要在 statusbar 下绘制布局:
values/styles.xml值/样式.xml
<item name="android:windowTranslucentStatus">true</item>
values-v21/styles.xml值-v21/styles.xml
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
Use CoordinatorLayout/DrawerLayout which already take care of the fitsSystemWindows parameter or create your own layout to like this:使用已经处理 fitsSystemWindows 参数的 CoordinatorLayout/DrawerLayout 或创建您自己的布局来像这样:
public class FitsSystemWindowConstraintLayout extends ConstraintLayout {
private Drawable mStatusBarBackground;
private boolean mDrawStatusBarBackground;
private WindowInsetsCompat mLastInsets;
private Map<View, int[]> childsMargins = new HashMap<>();
public FitsSystemWindowConstraintLayout(Context context) {
this(context, null);
}
public FitsSystemWindowConstraintLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FitsSystemWindowConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (ViewCompat.getFitsSystemWindows(this)) {
ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View view, WindowInsetsCompat insets) {
FitsSystemWindowConstraintLayout layout = (FitsSystemWindowConstraintLayout) view;
layout.setChildInsets(insets, insets.getSystemWindowInsetTop() > 0);
return insets.consumeSystemWindowInsets();
}
});
setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
TypedArray typedArray = context.obtainStyledAttributes(new int[]{android.R.attr.colorPrimaryDark});
try {
mStatusBarBackground = typedArray.getDrawable(0);
} finally {
typedArray.recycle();
}
} else {
mStatusBarBackground = null;
}
}
public void setChildInsets(WindowInsetsCompat insets, boolean draw) {
mLastInsets = insets;
mDrawStatusBarBackground = draw;
setWillNotDraw(!draw && getBackground() == null);
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
if (ViewCompat.getFitsSystemWindows(this)) {
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) child.getLayoutParams();
if (ViewCompat.getFitsSystemWindows(child)) {
ViewCompat.dispatchApplyWindowInsets(child, insets);
} else {
int[] childMargins = childsMargins.get(child);
if (childMargins == null) {
childMargins = new int[]{layoutParams.leftMargin, layoutParams.topMargin, layoutParams.rightMargin, layoutParams.bottomMargin};
childsMargins.put(child, childMargins);
}
if (layoutParams.leftToLeft == LayoutParams.PARENT_ID) {
layoutParams.leftMargin = childMargins[0] + insets.getSystemWindowInsetLeft();
}
if (layoutParams.topToTop == LayoutParams.PARENT_ID) {
layoutParams.topMargin = childMargins[1] + insets.getSystemWindowInsetTop();
}
if (layoutParams.rightToRight == LayoutParams.PARENT_ID) {
layoutParams.rightMargin = childMargins[2] + insets.getSystemWindowInsetRight();
}
if (layoutParams.bottomToBottom == LayoutParams.PARENT_ID) {
layoutParams.bottomMargin = childMargins[3] + insets.getSystemWindowInsetBottom();
}
}
}
}
}
requestLayout();
}
public void setStatusBarBackground(Drawable bg) {
mStatusBarBackground = bg;
invalidate();
}
public Drawable getStatusBarBackgroundDrawable() {
return mStatusBarBackground;
}
public void setStatusBarBackground(int resId) {
mStatusBarBackground = resId != 0 ? ContextCompat.getDrawable(getContext(), resId) : null;
invalidate();
}
public void setStatusBarBackgroundColor(@ColorInt int color) {
mStatusBarBackground = new ColorDrawable(color);
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDrawStatusBarBackground && mStatusBarBackground != null) {
int inset = mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0;
if (inset > 0) {
mStatusBarBackground.setBounds(0, 0, getWidth(), inset);
mStatusBarBackground.draw(canvas);
}
}
}
}
main_activity.xml main_activity.xml
<FitsSystemWindowConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/toolbar_background"
app:layout_constraintBottom_toBottomOf="@id/toolbar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Content"
android:textSize="48sp" />
</LinearLayout>
</FitsSystemWindowConstraintLayout>
Result :结果 :
For those who want a completely transparent status bar and navigation bar on KitKat and up there is a small conflict with using windowTranslucentNavigation
with @Machado answer's for Lollipop and to prevent that conflict separate the styles对于那些想要在 KitKat 和更高版本上使用完全透明的状态栏和导航栏的人,使用windowTranslucentNavigation
和 @Machado 的 Lollipop 答案存在小冲突,并防止这种冲突将样式分开
styles.xml样式文件
<style name="LockScreenStyle" parent="@android:style/Theme.Wallpaper.NoTitleBar">
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
<item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>
</style>
styles.xml (v21)样式.xml (v21)
<style name="LockScreenStyle" parent="@android:style/Theme.Wallpaper.NoTitleBar">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
MyClass.java我的类
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow(); // in Activity's onCreate() for instance
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
w.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
}
You can also change the alpha of your colorPrimary and colorPrimaryDark to 00 and then add this to your onCreateMethod:您还可以将 colorPrimary 和 colorPrimaryDark 的 alpha 更改为 00,然后将其添加到您的 onCreateMethod 中:
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
and also add this to your activity:并将其添加到您的活动中:
android:fitsSystemWindows="true"
This is for the new guys like myself on Nativescript + Angular.这适用于像我这样使用 Nativescript + Angular 的新手。 I started off with one of the blank templates on the NTS marketplace .我从NTS 市场上的一个空白模板开始。 Then For my specific use, I need the status bar on top to be always transparent (similar to the iOS style) and the navbar (bottom) to be fully transparent (not translucent!) on some components/modules, while opaque in others.然后对于我的特定用途,我需要顶部的状态栏始终透明(类似于 iOS 样式),并且导航栏(底部)在某些组件/模块上完全透明(不是半透明!),而在其他组件/模块上不透明。 If everything is done according to the protocol set by NTS, you should have a main.ts (a prerequisite for my solution) similar to this如果一切都按照 NTS 设置的协议完成,您应该有一个类似于此的 main.ts(我的解决方案的先决条件)
I separated my work into two pieces, top first, then the bottom.我把我的作品分成两部分,首先是顶部,然后是底部。 For the top I found this answer that worked.对于顶部,我发现这个答案有效。 So you should have a main.ts file that looks like this.所以你应该有一个如下所示的 main.ts 文件。
import { platformNativeScriptDynamic } from "@nativescript/angular";
import { AppModule } from "./app/app.module";
import * as application from "tns-core-modules/application";
import { Color } from "@nativescript/core";
platformNativeScriptDynamic().bootstrapModule(AppModule);
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);
activity.getWindow().setNavigationBarColor(android.graphics.Color.TRANSPARENT);
} else {
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().setNavigationBarColor(android.graphics.Color.TRANSPARENT);
}
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);
}
}
});
Then to add transparency to the navbar, I followed this and I add this to line 24 of the main.ts file above.然后为了向导航栏添加透明度,我按照此操作并将其添加到上面 main.ts 文件的第 24 行。 activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
Now mind you there are also the styles.xml files we have to respect, so my styles.xml file (App_Resources\\Android\\src\\main\\res\\values\\styles.xml) looks like this:现在请注意,还有我们必须尊重的styles.xml 文件,所以我的styles.xml 文件(App_Resources\\Android\\src\\main\\res\\values\\styles.xml)如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- theme to use FOR launch screen-->
<style name="LaunchScreenThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
<item name="toolbarStyle">@style/NativeScriptToolbarStyle</item>
<item name="colorPrimary">@color/ns_primary</item>
<item name="colorPrimaryDark">@color/ns_primaryDark</item>
<item name="colorAccent">@color/ns_accent</item>
<item name="android:windowBackground">@drawable/splash_screen</item>
<item name="android:statusBarColor">@color/transparent</item>
</style>
<style name="LaunchScreenTheme" parent="LaunchScreenThemeBase"></style>
<!-- theme to use AFTER launch screen is loaded-->
<style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
<item name="toolbarStyle">@style/NativeScriptToolbarStyle</item>
<item name="colorPrimary">@color/ns_primary</item>
<item name="colorPrimaryDark">@color/ns_primaryDark</item>
<item name="colorAccent">@color/ns_accent</item>
<item name="android:statusBarColor">@color/transparent</item>
<item name="android:navigationBarColor">@color/transparent</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>
<style name="AppTheme" parent="AppThemeBase"></style>
<!-- theme for action-bar -->
<style name="NativeScriptToolbarStyleBase" parent="Widget.AppCompat.Toolbar">
<item name="android:background">@color/transparent</item>
<item name="theme">@color/transparent</item>
<item name="popupTheme">@color/transparent</item>
</style>
<style name="NativeScriptToolbarStyle" parent="NativeScriptToolbarStyleBase"></style>
Took me about 2-3 days to figure out, hope this helps我花了大约 2-3 天才弄清楚,希望这会有所帮助
fun Activity.setTransparentStatusBar() { WindowCompat.setDecorFitsSystemWindows(window, false) window.statusBarColor = Color.TRANSPARENT window.navigationBarColor = Color.TRANSPARENT }<\/code><\/pre> That's it而已
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.