android studio 中使用kotlin语言 直接操作布局id

android studio 中使用kotlin语言 直接操作布局id

需要在 build.gradle 文件 引入

java 复制代码
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

(会自动生成,可忽略)然后在 Activity 文件中 引入 对应的 layout 文件 如:activity_main.xml

import kotlinx.android.synthetic.main.activity_main.*

然而,运行是会提示kotlin-android-extensions废弃

java 复制代码
【Android Studio】The 'kotlin-android-extensions' Gradle plugin is deprecated.

所以我们采用viewbinding来直接操作布局id

​

一、ViewBinding开启

在app下build.grade中加入以下代码

AS 3.6.x

java 复制代码
android {
	...
     viewBinding{
        enabled = true
  	}
}

AS 4.0.x

java 复制代码
android {
	...
    buildFeatures{
        viewBinding= true
    }
}

开启之后,创建xml布局文件时,编译器会自动化生成对应的xxxBinding文件。例如:activity_main.xml 对应生成ActivityMainBinding,下面进行代码演示

一、Activity中使用ViewBinding

1、创建MainActivity、activity_main.xml文件

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/transparent" />


    <FrameLayout
        android:id="@+id/fl_contain"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

2、Java中把自动生成的ActivityMainBinding进行填充绑定后再 setContentView(rootView);

java 复制代码
        //关键代码:binding xml布局
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        View rootView = mBinding.getRoot();
        setContentView(rootView);

3、绑定之后就可以直接通过mBinding.xxx(xxx==id)使用控件,完整代码如下:

java 复制代码
public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //关键代码:binding xml布局
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        View rootView = mBinding.getRoot();
        setContentView(rootView);

        //使用介绍
        mBinding.ivTest.setBackground(ContextCompat.getDrawable(this, R.mipmap.ic_launcher));

        mBinding.flContain.setOnClickListener(v -> {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(R.id.fl_contain, new BindViewFragment());
            transaction.commit();
        });
    }
}

​

相关推荐
小程故事多_801 小时前
从 Tokenizer 到 Next Token,穿透数据流,完整理解 Transformer 底层逻辑
android·深度学习·transformer
三少爷的鞋4 小时前
Android 协程实践:为什么不应将 `supervisorScope` 作为常规方案
android
承渊政道6 小时前
【从零开始大模型开发与微调:基于PyTorch与ChatGLM】(从ChatGLM本地化到LoRA、PEFT的高级微调)
android·人工智能·pytorch·chatglm·本地化部署·大模型开发与微调
倚楼听风语6 小时前
Kotlin 协程与 suspend 原理详解
java·算法·kotlin
2601_962885728 小时前
如何用 Python 判断一只股票“追涨”还是“抄底”更有效?
android·开发语言·python
素师良码8 小时前
第10篇:屏ESD Check内核逻辑驱动代码完整拆解
android
高晶8 小时前
Android 短信收发模块从零到实战:权限配置、界面设计、发送接收与兼容性适配全攻略
android·程序员
随遇丿而安8 小时前
第18周:ViewModel 基础功能 + 架构优化
android
问心无愧051311 小时前
[GXYCTF2019]Ping Ping Ping
android·笔记
事圆则缓11 小时前
Kotlin 集合与序列:给页面一份清楚的数据
开发语言·windows·kotlin