butterknife 具体是做什么的我就不废话了 有什么好处 我也不说了
简单步骤如下
1. 配置project的build.gradle
添加
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
如图所示
2. 配置moudle的gradle
添加
apply plugin: 'com.neenbedankt.android-apt'
然后在dependencies 节点添加
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
如图所示
3. 接下来就可以使用了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.hutao.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_id"/>
</LinearLayout>
package com.example.hutao.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.text_id) TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
title.setText("胡涛");
}
}
4 运行结果
注意:android studio 项目工程里是有两个build.gradle
其实还有种方法 如下图
这种方法最简单了,不用配置apt了