状态栏不透明,但是白色

用anko DSL测试kotlin,我决定使用kotlin插件(1.0.3)和最新的anko库(0.9)在最后的android studio ide(2.1.3)中启动一个新的proyect,

我使用默认的导航抽屉活动,所以我只需要将主要的XML转换为ANK。

这是xml:

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" > <TextView android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> </android.support.design.widget.CoordinatorLayout> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.DrawerLayout> 

它正在完美地工作,正如你在这里看到的那样: [XML]

与安科,我试图复制XML的每一个细节,得到这个代码:

 class MainActivityUi: AnkoComponent<MainActivity> { override fun createView(ui: AnkoContext<MainActivity>) = with(ui) { drawerLayout { id = R.id.drawer_layout fitsSystemWindows = true coordinatorLayout { appBarLayout(R.style.AppTheme_AppBarOverlay) { toolbar { id = R.id.toolbar backgroundColor = colorAttr(R.attr.colorPrimary) popupTheme = R.style.AppTheme_PopupOverlay }.lparams(height=dimenAttr(R.attr.actionBarSize),width=matchParent) }.lparams(width=matchParent) relativeLayout { padding = dip(16) textView("Hello World!") }.lparams(height=matchParent,width=matchParent) { behavior = AppBarLayout.ScrollingViewBehavior() } }.lparams(height=matchParent,width=matchParent) navigationView { id = R.id.nav_view inflateHeaderView(R.layout.nav_header_main) inflateMenu(R.menu.activity_main_drawer) }.lparams(height=matchParent) { gravity = Gravity.START fitsSystemWindows = true } } } } 

相反,我得到这个白色的状态栏: ANKO

我做的唯一更改是在MainActivity中将setContentView(R.layout.activity_main)更改为MainActivityUi.setContentView(this)。

所以,我的问题是,为什么当他们是相同的意见和布局时发生这种情况呢? 我该如何解决这个问题?

编辑:我使用默认的工程,它是在Android Studio中创建的,你选择新的工程,然后你选择DrawerNavigationActivity。 如果在setContentView中,我选择显示xml的视图,状态栏是蓝色的(第一个截图),但是如果我选择显示anko的视图,我会得到白色的状态栏。

在这两种情况下,我使用相同的主题,颜色等,当使用xml布局时,一切都运行完美,所以它必须是anko的问题

试着做

window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

在onCreate()

它为我工作

参考

编辑:也是我的代码

 drawerLayout { lparams(width = matchParent, height = matchParent) coordinatorLayout { setPadding(0, dip(24), 0, 0) appBarLayout { toolbar { backgroundColor = primaryColor setTitleTextColor(primaryColorText) }.lparams(width = matchParent, height = dip(toolbar_size)) }.lparams(width = matchParent, height = dip(toolbar_size)) }.lparams(width = matchParent, height = matchParent) navigationView { }.lparams(width = dip(302), height = matchParent) { gravity = Gravity.START } } 

你错误地认为状态栏默认是transparent的。

根据这个图像

在这里输入图像描述

取自Android开发人员参考。 检查: https : //developer.android.com/training/material/theme.html

状态栏颜色取决于您在colors.xml文件夹下的colors.xmlstyles.xml中定义的颜色colorPrimaryDark ,因此它不透明。

你的问题不是在状态栏中的white是正确的透明度,但没有足够的知识在Android应用程序中使用主题。

编辑:更改状态栏颜色(Java):

 Window window = activity.getWindow(); // clear FLAG_TRANSLUCENT_STATUS flag: window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // finally change the color window.setStatusBarColor(activity.getResources().getColor(R.color.my_statusbar_color)); 

要么

  public void setStatusBarColor(View statusBar,int color){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window w = getWindow(); w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //status bar height int actionBarHeight = getActionBarHeight(); int statusBarHeight = getStatusBarHeight(); //action bar height statusBar.getLayoutParams().height = actionBarHeight + statusBarHeight; statusBar.setBackgroundColor(color); } } 

希望它会有所帮助

这不是你的状态栏颜色问题。
纠正我,如果我错了,我相信你使用Cyanogenmod OS 13.0版的电池符号的外观。 将其切换回主题部分中的默认主题。 然后回报如果问题仍然存在与您的colors.xml文件。

请参阅此处的链接,了解Kotlin半透明状态栏的问题。
https://github.com/fython/MaterialStatusBarCompat