使用操作栏/工具栏和浮动按钮将MainActivity转换为Anko

时间:2015-12-11 02:22:49

标签: kotlin kotlin-android-extensions anko

我正在尝试学习如何使用Kotlin / Anko。

我已经通过示例text and code editor PSPad并且克隆了模板项目并且可以理解如何做一些基本的东西,但作为练习我想转换这个简单的活动(从Android Studio中的空白活动生成)并转换为Kotlin)也使用Anko。 Anko没有很多例子,大多数只是上面引用的github页面的副本。

有人可以演示如何将以下内容转换为Anko DSL吗?

MainActivity.kt

option explicit
'On Error Resume Next
On Error GoTo 0
Dim WshShell
set WshShell = WScript.CreateObject("WScript.Shell")
' WshShell.Run omitted, activate existing window (this script open in PSPad)
WScript.Sleep 100
WshShell.AppActivate "34189495.vbs"
WScript.Sleep 200
' Ctrl+Shift+3 set marker #3
WshShell.SendKeys "^3"
WScript.Sleep 100
' Ctrl+1 goto marker #1, i.e. move cursor to that line (pre)marked 1 
WshShell.SendKeys "^{+}"
' more time to observe 
WScript.Sleep 3000
' Ctrl+3 goto marker #3, i.e. move cursor to that line marked 3 
WshShell.SendKeys "{NUMLOCK}^{Š}{NUMLOCK}"
WScript.Sleep 100
' more time before quit
WScript.Sleep 3000
Wscript.Echo Wscript.ScriptName
Wscript.Quit

main_activity.xml

import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolBar = findViewById(R.id.toolbar) as Toolbar
        setSupportActionBar(toolBar)
        val fab = findViewById(R.id.fab) as FloatingActionButton
        fab.setOnClickListener { view -> Snackbar.make(view, "Replace this with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show() }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        if (id == R.id.action_settings) {
            println("settings clicked on ")
            return true
        }
        return super.onOptionsItemSelected(item)
    }
} 

content_main.xml

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

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.gmail.npnster.mykotlinfirstproject.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/hello"
        />
</RelativeLayout>

2 个答案:

答案 0 :(得分:6)

您可以使用ankoView方法在DSL上下文中创建View而不使用DSL方法。

例如,要创建NavigationView,可以使用

ankoView({ NavigationView(it) }) {
    lparams(width = wrapContent, height = matchParent, gravity = Gravity.START)
    // more initialization follows
}

这样您就可以实例化FloatingActionButtonAppBarLayout,只需在ankoView的第一个参数函数中调用它们的构造函数。为方便起见,您可以像the manual中那样使用类似DSL的功能:

fun floatingActionButton(init: FloatingActionButton.() -> Unit) = ankoView({ FloatingActionButton(it) }, init)

创建Toolbar更加容易:toolbar中有一个DSL org.jetbrains.anko.appcompat.v7方法。

使用Anko DSL时,要包含其他布局,就像使用content_main一样,可以使用Anko include功能,也可以只编写一个填充ViewGroup的函数。您可以使用此模板:

fun ViewGroup.myLayout() {
    textView("123")
    // more DSL code here
}

然后只需在myLayout()个初始化程序中调用ViewGroup

答案 1 :(得分:3)

我知道这是一个迟到的答案,但我希望它有助于某人。我这样做了布局(当然还需要一些样式):

class MainUI(val adapter: MainUIAdapter) : AnkoComponent<MainActivity> {

        override fun createView(ui: AnkoContext<MainActivity>): View = with(ui) {
        coordinatorLayout {
            fitsSystemWindows = true

            appBarLayout {
                toolbar {
                    setTitleTextColor(Color.WHITE) // so far still needed
                    id = R.id.toolbar
                }.lparams(width = matchParent, height = matchParent)

            }.lparams(width = matchParent)

            relativeLayout {
                id = R.id.container
                recyclerView { // just an example
                    id = R.id.recycler_view
                    adapter = this@MainUI.adapter
                    layoutManager = LinearLayoutManager(ctx)
                }
            }.lparams(width = matchParent, height = matchParent) {
                behavior = ScrollingViewBehavior()
            }
            floatingActionButton {
                onClick { doSomething() }
                imageResource = R.drawable.ic_add_white_24dp // the plus sign
            }.lparams {
                gravity = Gravity.BOTTOM or Gravity.END
                margin = dip(16)
            }
        }
    }
}

并在MainActivity中使用:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    MainUI(MainUIAdapter(people)).setContentView(this)

    toolbar = find<Toolbar>(R.id.toolbar)
    setSupportActionBar(toolbar)
}