In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use the Anko library in Kotlin, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand it.
Text
Android UI development has always been done using XML files. Although in theory, UI can be implemented in the Java language, it is not of much use. Not long ago, JetBrains launched Kotlin, a modern language for JVM, that can well implement Android UI.
Jetbrains claims that Anko is a faster and easier development style in Android. Kotlin provides the Anko library to design Android interfaces as DSL (domain-specific language). A simple example:
The following interface consists of a picture and a button:
The implementation using Anko is as follows:
VerticalLayout {imageView (R.drawable.anko_logo). Lparams (width= matchParent) {padding = dip (20) margin = dip (15)} button ("Tap to Like") {onClick {toast ("Thanks for the love!")}
We define a vertical linear layout as a container to contain pictures and buttons, use lparams to define the location information of the layout, and realize the button click event by the inline function of Kotlin.
Advantages of using Anko:
We can embed the UI layout into our code to make it type safe.
Because we don't use XML to write, it increases efficiency because CPU time is wasted analyzing XML.
After the programmatic transformation of UI, we can put the Anko DSL fragment into a function. This facilitates code reuse.
Obviously, the code is more concise, readable and manageable.
Now let's build a to-do app using Anko Layout and Kotlin to list what we need to do today.
You can find the project to-do app on GitHub.
Add the Anko library to Android Studio:
In streamline-android-java-code-with-kotlin to learn how to add Kotlin to your Android project, with Kotlin, we need to add Anko dependencies to app/build.gradle so that we can compile the project smoothly.
Compile [size=1em] 'org.jetbrains.anko:anko-sdk15:0.8.3' / / sdk19,21,23 can also be used
You can add this dependency based on the minSdkVersion of your project, as shown in the above example 15
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.