Anko
来源:互联网 发布:snmp trap协议端口号 编辑:程序博客网 时间:2024/06/08 13:46
anko
anko 是一款结合kotlin语言抛弃布局xml的工具,使用他就可以不用写布局xml代码了
举个栗子
如果我们需要实现一个功能 点击按钮toast弹出输入框输入的字符(如下效果图)
效果图
普通方式实现这样一个功能,可能我们会这么做
- 创建布局xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.github.jokar.ankotest.MainActivity"> <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="showEdit" android:onClick="showEdit"/></LinearLayout>
- 在activity里初始化输入框和 写点击事件
class MainActivity : AppCompatActivity() { var edit: EditText? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) edit = findViewById(R.id.edit) as EditText } fun showEdit(v: View) { var string = edit?.text.toString() Toast.makeText(applicationContext, string, Toast.LENGTH_SHORT).show() }}
但是如果我们使用anko就不同了,我们不需要XML文件了,然后修改activity如下
class MainActivity : AppCompatActivity() { var edit: EditText? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) createView() } fun showEdit() { var string = edit?.text.toString() Toast.makeText(applicationContext, string, Toast.LENGTH_SHORT).show() } fun createView() { linearLayout { lparams(width = matchParent, height = matchParent) orientation = LinearLayout.VERTICAL edit = editText { textSize = 15f }.lparams(width = matchParent, height = wrapContent) button { text = "ShowText" textSize = 15f onClick { showEdit() } }.lparams(width = matchParent, height = wrapContent) { topMargin = dip(10) } } }}
这就是anko的基本用法了,跟写xml布局没有多大区别;但在实际使用过程中会发现如果不是再activity
类里想直接使用anko控件是不行的;;下面介绍开发中遇到的基本场景
在fragment里使用anko
在fragment里使用还是比较容易的;只需要在最外层加上UI {}.view
就可以使用了;样例如下:
这样就行了,使用很简单
class Fragment1 : Fragment() { var swipeRefreshLayout: SwipeRefreshLayout? = null var recyclerView: RecyclerView? = null override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { return super.onCreateView(inflater, container, savedInstanceState) } /** * createView */ fun createView(): View { return UI { linearLayout { //swipeRefreshLayout swipeRefreshLayout = swipeRefreshLayout { setColorSchemeResources(android.R.color.holo_blue_bright) setOnRefreshListener { getData() } //recyclerView recyclerView = recyclerView { layoutManager = LinearLayoutManager(context) backgroundColor = Color.parseColor("#f3f3f3") } }.lparams(width = matchParent, height = matchParent) } }.view } fun getData() { }}
在Adapter里使用
这里介绍如何在非activity,fragment里使用anko,也很简单使用到了with
字符,例如这样:
fun createItemView(context: Context): View { return with(context) { linearLayout { lparams(width = matchParent, height = wrapContent) cardView { linearLayout { id = R.id.percentFrameLayout backgroundResource = selectableItemBackground(context) lparams(width = matchParent, height = dip(80)) orientation = LinearLayout.VERTICAL textView { id = R.id.text gravity = Gravity.LEFT textSize = 17f textColor = Color.BLACK }.lparams(width = matchParent, height = matchParent) { setPadding(dip(10), dip(10), dip(10), dip(10)) } } }.lparams(width = matchParent, height = wrapContent) { margin = dip(5) } } } } fun selectableItemBackground(context: Context): Int { var outValue = TypedValue() context.theme.resolveAttribute(android.R.attr.selectableItemBackground, outValue, true) return outValue.resourceId }
- 注意这里给控件设置了id,所以你需要在res/value文件夹下创建一个名为
ids.xml
文件,然后添加你所需要的id
<?xml version="1.0" encoding="utf-8"?><resources> <item name="percentFrameLayout" type="id"/> <item name="text" type="id"/></resources>
然后在adapter里使用就是这样:
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { return ViewHolder(createItemView(context)) } inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { var tvTitle = itemView.findViewById(R.id.text) as TextView }
引用xml布局文件
当然anko也不是那么死板的工具,他也可以引用xml布局文件很简单;使用include
字符就行
include<View>(R.layout.common_toolbar)
就像这样就可以了
高级用法 - 如何使用自定义view
在实际开发中anko给我们提供的控件肯定是不能满足我们的需求的,那我们怎么把自己的自定义控件使用到anko中呢,其实很简单
- 首先创建一个
Views.kt
文件 - 在该文件中添加下面代码
inline fun ViewManager.customizeView(theme: Int = 0) = customizeView(theme) {}inline fun ViewManager.customizeView(theme: Int = 0, init: CustomizeView.() -> Unit) = ankoView({ CustomizeView(it) }, theme, init)
* 这里的```CustomizeView```是你的自定义View * 这里的```customizeView```是你用在anko里的名称,一般就是自定义view的名称,首字母小写这样的写法 然后就可以在anko里这样使用了customizeView{}
anko的基本使用就到这了,后续若遇到其他问题会补充上的;若 想看anko具体在项目里使用;可以看看一个使用 kotlin编写的知乎日报 .
阅读全文
0 0
- Anko
- Kotlin + anko
- Anko初探
- Anko for Android
- 使用anko 抛弃XML
- Kotlin Anko简单使用
- Android Kotlin anko 标题居中
- anko dsl preview plugin 错误
- Kotlin&Anko基础教程大全 一
- Kotlin&Anko基础教程大全 二
- Kotlin&Anko, 扔掉XML开发Android应用
- Anko和扩展的函数(六)
- android 3.0 kotlin开发(1) 引入 anko
- 带你全方位使用Anko库-上篇
- 带你全方位使用Anko库-下篇
- android studio3.0 kotlin配置Anko
- Android studio2.3.2 配置kotlin、Anko
- Anko demo(二) :CustomView 首次尝试
- (POJ
- Python爬虫入门:Urllib parse库使用详解(二)
- Android 之多种网络请求实现方式
- 神经机器翻译(Neural Machine Translation)系列教程
- HDU3091 Necklace 【状压】
- Anko
- 使用 Crossfilter 和 dc.js 构建交互式报表
- POJ 2528 Mayor's posters (线段树+离散化)
- JAVA企业面试题精选 Web基础 41-42
- HDU
- 机器学习:AI资源网址链接(大牛,研究机构,视频,博客,书籍,Quora......)
- 计算机的原码, 反码和补码
- 需要添加的依赖
- python之语法