如何使用Anko DSL设置NavigationView的HeaderView?

3

通常在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">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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>

我尝试使用Anko DSL编写这段代码:

...

override fun createView(ui: AnkoContext<MainActivity>) = with(ui) {
    drawerLayout {
        lparams(width = matchParent, height = matchParent)
        id = ID_DRAWER_LAYOUT
        fitsSystemWindows = true

        navigationView {
            lparams(width = wrapContent, height = matchParent)
            id = ID_NAVIGATION_VIEW
            foregroundGravity = Gravity.START
            fitsSystemWindows = true

            addHeaderView(navHeaderView()) //// PROBLEM HERE ////
            inflateMenu(R.menu.activity_main_drawer)
        }
    }
}

private fun ViewGroup.navHeaderView() {
    linearLayout {
        lparams(width = matchParent, height = dip(160))
        backgroundResource = R.drawable.side_nav_bar
        gravity = Gravity.BOTTOM
        orientation = LinearLayout.VERTICAL
        setPadding(dip(64), dip(16), dip(64), dip(16))

        imageView {
            id = ID_IMAGE_VIEW
            topPadding = dip(16)
            imageResource = android.R.drawable.sym_def_app_icon
        }

        textView {
            lparams(width = matchParent, height = wrapContent)
            id = ID_TEXT_NAME
            topPadding = dip(16)
            text = "test1"
            if (Build.VERSION.SDK_INT >= 23) {
                setTextAppearance(R.style.TextAppearance_AppCompat_Body1)
            } else {
                setTextAppearance(context, android.R.style.TextAppearance_Medium)
            }
        }

        textView {
            id = ID_TEXT_DESCRIPTION
            text = "test2"
        }
    }
}

...

这条标记线会抛出一个异常,表示该视图已经有父级。


然后我尝试使用 Anko DSL 预览插件自动转换 xml,但它只是这样做了,没有完全将其转换为 Anko DSL:

android.support.v4.widget.DrawerLayout {
    id = Ids.drawer_layout

    include<View>(R.layout.app_bar_main).lparams(width = matchParent, height = matchParent)
    org.mewx.projectprpr.template.NavigationFitSystemView {
        id = Ids.nav_view
        app:headerLayout = @layout/nav_header_main //// HERE ////
        app:menu = @menu/activity_main_drawer
    }.lparams(width = wrapContent, height = matchParent)
}

我该如何使用Anko DSL添加头部视图?
谢谢!
1个回答

3

首先,DrawerLayout在构造函数中检查fistSystemWindows并设置状态栏背景。因此,与fitsSystemWindows一起使用DrawerLayout的最简单方法是从XML文件中膨胀它。

其次,Anko DSL方法不仅会创建新视图,还会将它们附加到父视图中。您必须创建一个分离的视图:

addHeaderView(UI {
    navHeaderView()
}.view)

这段代码适用于Anko 0.10.0-beta-2,因版本不同,其行为和语法也可能有所不同。
其次,我遇到了一个问题:当我在键盘显示的情况下打开抽屉时,标题视图会出现大量底部填充(缩进)。我的解决方法如下:
addHeaderView(
  object : _RelativeLayout(context) { // fixes bottomPadding with open keyboard :'(
    @TargetApi(20) override fun onApplyWindowInsets(insets: WindowInsets) =
        insets.consumeSystemWindowInsets()
  }.apply {
    /* DSL code here */
  })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接