使用anko 抛弃XML

来源:互联网 发布:leg圣兽dbc数据 编辑:程序博客网 时间:2024/05/18 02:29

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编写的知乎日报 ;;若觉得对你有用欢迎点赞和star

原创粉丝点击