为什么这个anko布局不正确地处理系统ui?

我尝试了Anko,不能得到这个布局来处理像xml版本的系统用户界面。 我认为它与主题有关,以及它们是如何通过xml解析来处理的。

这里是截图:

FIM SIM卡

这里是anko DSL组件:

class ActivityMainLayout : AnkoComponent<Activity> { lateinit var drawerLayout: DrawerLayout lateinit var appBarLayout: AppBarLayout lateinit var toolbar: Toolbar lateinit var text: TextView lateinit var navHeader: NavigationView lateinit var toggle: ActionBarDrawerToggle override fun createView(ui: AnkoContext<Activity>): View = with(ui) { drawerLayout = drawerLayout { fitsSystemWindows = true coordinatorLayout { appBarLayout = appBarLayout(theme = R.style.AppTheme_AppBarOverlay) { toolbar = toolbar { popupTheme = R.style.AppTheme_PopupOverlay backgroundColor = getColorPrimary(ui.owner) }.lparams(width = matchParent, height = getActionBarSize(ui.owner)) }.lparams(width = matchParent) nestedScrollView { text = textView { text = "Anko Version" textSize = 40f }.lparams { margin = dip(16) } }.lparams(width = matchParent) { behavior = AppBarLayout.ScrollingViewBehavior() } }.lparams(width = matchParent, height = matchParent) navHeader = navigationView { inflateHeaderView(R.layout.nav_header_test_design) inflateMenu(R.menu.activity_test_design_drawer) }.lparams(height = matchParent) { gravity = Gravity.START } } toggle = ActionBarDrawerToggle( ui.owner, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) return drawerLayout } fun getActionBarSize(ctx: Context): Int { val attrs = IntArray(1) attrs[0] = attr.actionBarSize val array = ctx.obtainStyledAttributes(attrs) val size = array.getDimensionPixelSize(0, 0) array.recycle() return size } fun getColorPrimary(ctx: Context): Int { val attrs = IntArray(1) attrs[0] = attr.colorPrimary val array = ctx.obtainStyledAttributes(attrs) val color = array.getColor(0, 0) array.recycle() return color } 

这里是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 android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" 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> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:textSize="40dp" android:text="XML Version" /> </android.support.v4.widget.NestedScrollView> </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_test_design" app:menu="@menu/activity_test_design_drawer" /> </android.support.v4.widget.DrawerLayout> 

这是styles.xml:

 <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> </resources> 

我已经设法通过从drawerLayout { ... }块中删除fitsSystemWindows = true来解决问题。

我在coordinatorLayout { ... }navigationView { ... }块中保持了fitsSystemWindows = true 。 通过这个改变,StatusBar是透明的,并且在AppBar顶部保留了正确的填充空间。

但是,我发现另一个问题依然存在: AppBar本身在Android StatusBar(?!)后面滚动 ,这是不应该发生的事情…

我已经通过在appBarLayout { ... }lparams块中添加behavior = AppBarLayout.ScrollingViewBehavior() (同样可以应用于themedAppBarLayout )来解决这个问题,如下所示:

 themedAppBarLayout(theme = R.style.AppTheme_AppBarOverlay) { toolbar() { id = R.id.toolbar popupTheme = R.style.AppTheme_PopupOverlay backgroundResource = R.color.colorPrimary }.lparams(width = matchParent, height = dimenAttr(R.attr.actionBarSize)) }.lparams(width = matchParent) { behavior = AppBarLayout.ScrollingViewBehavior() } 

这使得AppBar被修复,不再滚动(因为它应该是)。

在GitHub的Anko项目的这个问题上,这两个问题都可能是相关的,也可以参考: NavigationDrawer使状态栏变成白色背景·Issue#298·Kotlin / anko

这适用于Android Studio 3.0 Beta 2,Kotlin 1.1.4-2和Anko 0.10.1。