You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio(Kotlin)中实现Material Design 3?含基础疑问

Material Design基础与Kotlin实现指南

1. Material Design是什么?

它是Google推出的一套现代化设计系统,不只是零散的UI组件,而是包含视觉风格、交互规则、动效规范的完整设计语言,目的是让Android(甚至跨平台)应用拥有统一、流畅且符合用户预期的体验。比如你常见的圆角卡片、点击波纹、悬浮按钮(FAB)、带提示的输入框,都是Material Design的典型元素。

2. Material Design是否默认集成在Android Studio中?

新创建的Android项目(比如用Empty Activity模板)会默认引入Material组件的依赖,但如果是旧项目、自定义模板或者手动精简过依赖的项目,可能需要自己添加。

3. 用Kotlin在Android Studio中实现Material Design组件的步骤

第一步:确认依赖

打开模块级的build.gradle文件(注意是Module而非Project级),检查是否有Material组件的依赖:

dependencies {
    implementation 'com.google.android.material:material:<最新稳定版本号>'
}

如果没有,直接加上就行,版本号用官方最新的稳定版即可。

第二步:切换到Material主题

要让Material组件生效,必须把App的主题改成Material系列主题。打开res/values/styles.xml,修改主题的父类:

<style name="Theme.YourAppName" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- 自定义主题属性,比如颜色、标题栏样式等 -->
</style>

也可以选择NoActionBar版本,根据你的需求来。

第三步:布局中使用Material组件

直接在XML布局里替换原生组件为Material版本就行,举几个常用的例子:

<!-- 带提示的输入框 -->
<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入用户名">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>

<!-- Material风格按钮 -->
<com.google.android.material.button.MaterialButton
    android:id="@+id/material_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="确认"
    app:cornerRadius="8dp"/> <!-- 自定义圆角 -->

<!-- 卡片布局 -->
<com.google.android.material.card.MaterialCardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="4dp"
    app:cardCornerRadius="12dp">

    <!-- 卡片内部内容 -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是Material卡片"
        android:padding="16dp"/>
</com.google.android.material.card.MaterialCardView>

第四步:Kotlin代码中交互

和原生组件的用法完全一致,用findViewById或者更推荐的ViewBinding来获取组件,处理事件:

// 用findViewById的方式
val materialBtn = findViewById<MaterialButton>(R.id.material_btn)
materialBtn.setOnClickListener {
    // 点击后执行的逻辑
    Toast.makeText(this, "按钮被点击了", Toast.LENGTH_SHORT).show()
}

// 用ViewBinding的方式(需要先启用ViewBinding)
binding.materialBtn.setOnClickListener {
    Toast.makeText(this, "按钮被点击了", Toast.LENGTH_SHORT).show()
}

另外你提到的那个仓库,其实是完全支持Kotlin的,它的示例代码里既有Java也有Kotlin版本,组件本身是跨语言的,不用纠结仓库的语言,直接用Kotlin调用就行。

内容的提问来源于stack exchange,提问作者Lluis McLaine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 00:05:35