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();
        });
    }
}

相关推荐
Android-Flutter30 分钟前
android fragment 使用
android·kotlin
迷茫中的自我1 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·人工智能
随遇丿而安1 小时前
第13周:页面状态保存 + 数据恢复优化
android
万事可爱^2 小时前
Claude 新发布的 Opus 5,系统提示语删了 80%,半价还能逼近 Fable 5
android·服务器·数据库·人工智能·claude
alexhilton2 小时前
响应式的Android身份验证架构
android·kotlin·android jetpack
喵都学不动了3 小时前
Android 自动化测试完全指南(新人版)
android
码云骑士3 小时前
76-全量微调vs-LoRA-vs-QLoRA-三种微调方式对比与选型
android
光头闪亮亮4 小时前
Fyne ( go跨平台GUI )项目实战-WebView 组件开发技术详解
android·go
嵌入式小周4 小时前
Genymotion 安卓模拟器在 Intel 芯片 Mac 上的运行(附带下载方式)
android·macos
zzq77976 小时前
Android 16 API 36 升级后 APP 加固兼容性问题解析
android·开发语言·安全·kotlin·安卓·安全架构