编辑
使用Anko减少布局代码
本文访问次数:0
  1. 1. 简介
  2. 2. 配置
    1. 2.1. minSdkVersion
    2. 2.2. Support Library
    3. 2.3. 完整的例子
  3. 3. GitHub

简介

Anko使Android开发变得更加简单快捷

配置

minSdkVersion

首先需要确定项目的minSdkVersion,并按照下面的说明进行配置

  • org.jetbrains.anko:anko-sdk15 for 15 <= minSdkVersion < 19
  • org.jetbrains.anko:anko-sdk19 for 19 <= minSdkVersion < 21
  • org.jetbrains.anko:anko-sdk21 for 21 <= minSdkVersion < 23
  • org.jetbrains.anko:anko-sdk23 for 23 <= minSdkVersion

Support Library

可选的Support Library

  • org.jetbrains.anko:anko-support-v4
  • org.jetbrains.anko:anko-appcompat-v7
  • org.jetbrains.anko:anko-cardview-v7
  • org.jetbrains.anko:anko-gridlayout-v7
  • org.jetbrains.anko:anko-recyclerview-v7
  • org.jetbrains.anko:anko-design
  • org.jetbrains.anko:anko-percent

完整的例子

buildscript {
    ext.kotlin_version = '1.1.2-3'
    ext.anko_version = '0.9.1'
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "me.zongren.ankodemo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.anko:anko-sdk15:$anko_version"

    compile "org.jetbrains.anko:anko-support-v4:$anko_version"
    compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
    compile "org.jetbrains.anko:anko-cardview-v7:$anko_version"
    compile "org.jetbrains.anko:anko-gridlayout-v7:$anko_version"
    compile "org.jetbrains.anko:anko-recyclerview-v7:$anko_version"
    compile "org.jetbrains.anko:anko-design:$anko_version"
    compile "org.jetbrains.anko:anko-percent:$anko_version"
}

GitHub

AnkoDemo

需要输入验证码才能留言

没有任何评论