利用ButterKnife来实现利用注解绑定控件。
-
概述 开发环境:Android studio 依赖组件:butterknife 依赖插件 :Android ButterKnife Zelezny
-
步骤:
- 在gradle中添加文件依赖: compile 'com.jakewharton:butterknife:7.0.1'
- 添加 Android ButterKnife Zelezny 插件 file-->setting-->Plugins--> 查找Android Butterknfie Zelezny,安装完成后重启Android Studio
-
示例程序: XML代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bindtest.MainActivity">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/tv_test"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/edit_test"/> </RelativeLayout>
在setConTentView上边右击,选择Generate
选择Generate Butterknife Injections
最后选择需要的选项,提交。
完成控件绑定