Kotlin 配置dataBinding

首先在app.build中配置 我出示下我得配置

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
//加入kapt 插件
apply plugin: 'kotlin-kapt'

android {
//    dataBinding.enable = true
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.g.kotiledemo"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

//开启databinding
    dataBinding {
        enabled true
    }

    kapt {
        generateStubs = true
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    //配置kapt
    "com.android.databinding:compiler:3.1.2"
}

等待gradle 导入完成后,开始编写自己得布局

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <data>

        <import type="com.g.kotiledemo.Userinino"></import>

        <variable
            name="userinfp"
            type="Userinino"></variable>
    </data>

    <LinearLayout
        tools:context=".MainActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <TextView
            android:id="@+id/num"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@{userinfp.name}"
            android:textColor="#000"
            android:textSize="14sp" />

        <com.g.kotiledemo.view.LeftLineLayout
            android:id="@+id/left_mian_line"
            android:layout_width="match_parent"
            android:layout_height="56dp"></com.g.kotiledemo.view.LeftLineLayout>


    </LinearLayout>

加入了layout 标签和data 标签

这是我得bean 文件  未使用Kotlin

public class Userinino extends BaseObservable {

    private  String name;
    private  String passwrof;

    public Userinino(String name, String passwrof) {
        this.name = name;
        this.passwrof = passwrof;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPasswrof() {
        return passwrof;
    }

    public void setPasswrof(String passwrof) {
        this.passwrof = passwrof;
    }
}

bean文件对应在打他标签中

然后就是主要得Activity得配置

public class Userinino extends BaseObservable {

    private  String name;
    private  String passwrof;

    public Userinino(String name, String passwrof) {
        this.name = name;
        this.passwrof = passwrof;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPasswrof() {
        return passwrof;
    }

    public void setPasswrof(String passwrof) {
        this.passwrof = passwrof;
    }
}

最后得效果

猜你喜欢

转载自blog.csdn.net/a1033479126/article/details/90173616