Butter Knife 控件绑定
来源:互联网 发布:淘宝买刀 编辑:程序博客网 时间:2024/05/21 17:21
1.导包及设置
将下方代码添加到对应的地方
compile 'com.jakewharton:butterknife:8.8.1'annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
apply plugin: 'com.jakewharton.butterknife'
mavenCentral()
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
例子:
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' //控件绑定jar classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}allprojects { repositories { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}
apply plugin: 'com.android.application'//控件绑定apply plugin: 'com.jakewharton.butterknife'android { compileSdkVersion 25 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.imsjw" 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' //butterknife(控件绑定) compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' //测试 testCompile 'junit:junit:4.12'}
2.初始化
import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import butterknife.ButterKnife;public abstract class BaseActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(getContentViewId()); ButterKnife.bind(this); } public abstract int getContentViewId();}
3.构建布局
<?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:orientation="vertical"> <Button android:id="@+id/testBtn" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮" /></LinearLayout>
4.使用
import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.widget.LinearLayoutManager;import com.imsjw.R;import com.imsjw.adpater.IndexNavAdpater;import com.imsjw.base.BaseActivity;import com.imsjw.entity.VideoWeb;import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;import java.util.ArrayList;import java.util.List;import butterknife.BindView;public class MainActivity extends BaseActivity { @BindView(R.id.testBtn) Button testBtn; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public int getContentViewId() { return R.layout.index_activity; }}
阅读全文
0 0
- Butter Knife 控件绑定
- Butter Knife@BindView控件空指针问题
- Butter Knife
- Butter Knife
- Butter Knife
- Butter Knife
- Butter Knife
- Butter Knife
- Butter Knife
- Butter Knife
- Butter Knife
- Butter Knife
- Butter Knife使用教程
- Butter Knife在adapter
- Butter Knife 和 AndroidAnnotations
- Butter Knife 使用方法
- Butter Knife 的使用方法
- Android Butter Knife使用说明
- 京东手机图片爬取
- Storm架构图
- 谦先生的程序员日志之我的大数据生涯(1)
- CentOS 6.5下-- jdk及eclipse安装
- Linux 下文件完全复制(属性不变)
- Butter Knife 控件绑定
- 安卓软键盘相关技术文章推荐
- nginx配置location总结及rewrite规则写法
- log4j日志配置详解
- mybatis操作mariadb驱动的mysql批量插入报错java.nio.BufferOverflowException: null
- Apache PHP mairadb 分在3台服务器上
- 文档总结7-linux进程优先级及信号
- Axure怎么制作页面滚动时菜单固定在顶部的
- Java中indexof()的用法