ActionBar 和 Toolbar

1 基本概念和历史背景

1.1 ActionBar(Android 3.0 原生支持)

Android 3.0 引入原生顶部导航栏,提供标题、导航按钮、操作按钮等功能。集成方式依赖 Activity 的 setActionBar() 方法:

局限性:

  • 样式和布局定制性低,只能固定在顶部,无法满足复杂的设计需求;
  • 在 Android 5.0(Lollipop)后逐渐被 Toolbar 替代;

默认配置:

themes.xml 文件:

xml 复制代码
<resources xmlns:tools="http://schemas.android.com/tools">

    <style name="SuperUI" parent="Theme.Material3.DayNight">

    </style>

</resources>

在清单文件中配置主题:

xml 复制代码
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/SuperUI">
  	
  	......

</application>
1.2 Toolbar(Android 5.0+ 推荐方案)

作为 ActionBar 的替代品,是 android.appcompat.widget.Toolbar 包中的自定义 View。作为普通的 View 嵌入布局,通过 setSupportActionBar() 关联。

优势:

  • 完全自定义布局(可以在顶部、底部、侧边)、样式、动画和交互逻辑;
  • 支持跨版本兼容(通过 AppCompact 库在低版本系统中使用);

2 Toolbar 的核心用法和示例

2.1 移除 ActionBar 相关的主题属性

themes.xml 文件:

xml 复制代码
<!-- Base application theme. -->
<style name="SuperUI.NoActionBar" parent="Theme.Material3.DayNight.NoActionBar">

</style>

在清单文件中配置:

xml 复制代码
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/SuperUI">
  	
  	......

</application>
2.2 布局集成

在 XML 布局中添加 Toolbar:

xml 复制代码
<androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/holo_blue_bright"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
2.3 在 Activity 中关联 Toolbar
kotlin 复制代码
class TestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 获取 Toolbar 实例
        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        // 重点:设置为 ActionBar
        setSupportActionBar(toolbar)

        supportActionBar?.apply {
            title = "主界面"
            subtitle = "欢迎使用"
            setDisplayHomeAsUpEnabled(true) // 显示返回箭头
            setHomeAsUpIndicator(R.drawable.arrow_back) // 自定义返回图标
        }

    }
}
相关推荐
hunterandroid38 分钟前
Android 内存泄漏排查实战:从 LeakCanary 报警到根因定位
android·前端
我命由我123451 小时前
Android 设备的日志缓冲区被写满,logcat: Unexpected EOF!
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
171320330计算机毕设编程1 小时前
基于SpringBoot的在线拍卖系统
android·java·spring boot·小程序·课程设计
用户0934077735141 小时前
HarmonyOS WPS Open SDK 实践:OpenFileRequest 打开链路与沙箱拷贝
android·typescript·harmonyos
0xBADCODE1 小时前
动态DEX加载+反射+DES硬编码密钥:安卓三层逆向实战
android·java·python·安全·网络安全·逆向·ctf
Patrick在香港2 小时前
Python 拉取 C&SD 官方 API:香港 2022 年已跨过“超老龄线“,而抚养比正在爬回 1961
android·c语言·python·数据分析·时序数据库·数据可视化·香港
mmsx2 小时前
osmdroid 地图实战 03|谷歌影像的 URL,为什么不能 setTileSource 了事?
android
hai_android2 小时前
Kotlin 协程上下文(CoroutineContext)深度解析
android
恋猫de小郭3 小时前
Flutter A2UI 深度解析,它是怎么提供动态生产力的,然后为什么 A2UI 不只是 Flutter
android·前端·flutter
心平气和量大福大3 小时前
android-控件-单选框RadioButton
android