Anko 练手 demo,顺便优化之前的代码布局

前言

先是 Kotlin,再是 Anko,如果说接触 Kotlin 不单单是为了学习一门新的语言,并且 Google 也将 Kotlin 扶上了正妻的位置,学习 Kotlin 就是大势所趋,至少你要能看得懂,毕竟就像 AS 替代 Eclipse 一样,现在的第三方库基本都是 AS 的版本,可以想见不用多久,第三方库都会变成 Kotlin + Anko

尽管现阶段我对于 Anko 的使用少的可怜,更不用说理解了,不过单单优化布局这一个特性,我就觉得是很有吸引力的,并不是为了迎合 Anko 才这么说,我想有些经验的开发者都用过 ViewStub 这个类,这个类主要解决的问题就是:太过复杂的 xml 解析的时候会有延迟,导致 Activity 的 onCreate 很慢。而 Anko 的 DSL 其实本质上就是在用代码来写布局,毫无疑问,这直接就节省了解析 xml 并将其转化为代码的时间了,到了现在 Android O 都已经出来了,对于 App 的每一点优化,都是难能可贵的。

DEMO

这里写图片描述

题外话(坑):Anko 我遇到的坑 (应该会持续更新,PS:给自己定个小目标,然后坐等打脸)

原 xml ,省略了大量的TextView的篇幅
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#F8F8F8"
              android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/header_bar_height">

        <include
            android:id="@+id/simple_action_bar_back"
            layout="@layout/component_back_btn"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:maxWidth="300dp"
            android:singleLine="true"
            android:text="用户协议与隐私条款"
            android:textColor="#333333"
            android:textSize="@dimen/font_size_lg"/>
    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            ......
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:text="感谢您使用我们的产品和服务。"
                android:textColor="#333333"
                android:textSize="13sp"/>
            ......

        </LinearLayout>

    </ScrollView>

</LinearLayout>

Anko 布局

class KtUserContractActivityUi<KtUserContractActivity> : AnkoComponent<KtUserContractActivity> {

    companion object {
        val text1 = "感谢"
        .....
        val text48 = "保留一切解释和修改权利。"
    }

    private val customStyle = { v: Any ->
        when (v) {
            is TextView -> {
                v.textSize = 14f
                v.textColor = R.color.black
            }
        }
    }

    override fun createView(ui: AnkoContext<KtUserContractActivity>) = with(ui) {
        verticalLayout {
            backgroundColor = Color.parseColor("#FFFFFF")
            relativeLayout {
                include<View>(R.layout.component_back_btn)
                textView("用户协议与隐私条款") {
                    maxWidth = dip(300)
                    singleLine = true
                    textColor = Color.parseColor("#333333")
                    textSize = 20f
                }.lparams(width = wrapContent, height = wrapContent) {
                    centerInParent()
                }
                include<View>(R.layout.component_color_line)
            }.lparams(width = matchParent, height = dip(45))
            scrollView {
                verticalLayout {
                    textView(text1)
                    textView(text2).lparams {
                        topMargin = dip(25)
                    }
                    ......
                    textView(text48).lparams {
                        topMargin = dip(25)
                    }
                }.lparams(width = matchParent, height = matchParent) {
                    topPadding = dip(10)
                    horizontalMargin = dip(10)
                    bottomPadding = dip(10)
                }
            }.lparams(width = matchParent, height = matchParent){
            }.applyRecursively(customStyle)
        }
    }
}

Activity 代码

/**
 * Created by *** on 2017/5/27.
 */
class KtUserContractActivity : KtTitleBaseActivity() {

    override fun ui() {
        KtUserContractActivityUi<KtUserContractActivity>().setContentView(this);
    }

    companion object Launch {
        fun launch(context: Activity) = context.startActivity(Intent(context, KtUserContractActivity::class.java))
    }

}

代码优化 html

摒弃之前使用几十个TextView 构成的xml 或者 Anko,打算写一个 html ,使用一个 TextView 来搞定这个页面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值