我正在尝试 Anko,但无法让这种布局像 xml 版本那样处理系统 ui。我认为它与主题以及它们如何通过 xml 解析处理有关。
这是 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>