
Android - Add a Floating Action Button
发布日期:2021-05-06 23:02:40
浏览次数:37
分类:精选文章
本文共 2594 字,大约阅读时间需要 8 分钟。
文章目录
准备
IDE:
Android Studio 4.1.1Build #AI-201.8743.12.41.6953283, built on November 5, 2020Runtime version: 1.8.0_242-release-1644-b01 amd64VM: OpenJDK 64-Bit Server VM by JetBrains s.r.oWindows 10 10.0
Android Virtual Devices:
Name: Pixel_2_API_28CPU/ABI: Google Play Intel Atom (x86)Path: C:\Users\86188\.android\avd\Pixel_2_API_28.avdTarget: google_apis_playstore [Google Play] (API level 28)Skin: pixel_2SD Card: 512Mfastboot.chosenSnapshotFile: runtime.network.speed: fullhw.accelerometer: yeshw.device.name: pixel_2hw.lcd.width: 1080hw.initialOrientation: Portraitimage.androidVersion.api: 28tag.id: google_apis_playstorehw.mainKeys: nohw.camera.front: emulatedavd.ini.displayname: Pixel 2 API 28hw.gpu.mode: autohw.ramSize: 1536PlayStore.enabled: truefastboot.forceColdBoot: nohw.cpu.ncore: 4hw.keyboard: yeshw.sensors.proximity: yeshw.dPad: nohw.lcd.height: 1920vm.heapSize: 256skin.dynamic: yeshw.device.manufacturer: Googlehw.gps: yeshw.audioInput: yesimage.sysdir.1: system-images\android-28\google_apis_playstore\x86\showDeviceFrame: yeshw.camera.back: virtualsceneAvdId: Pixel_2_API_28hw.lcd.density: 420hw.arc: falsehw.device.hash2: MD5:55acbc835978f326788ed66a5cd4c9a7fastboot.forceChosenSnapshotBoot: nofastboot.forceFastBoot: yeshw.trackBall: nohw.battery: yeshw.sdCard: yestag.display: Google Playruntime.network.latency: nonedisk.dataPartition.size: 6442450944hw.sensors.orientation: yesavd.ini.encoding: UTF-8hw.gpu.enabled: yes
注意:以下示例仅在安卓虚拟设备上运行测试,并没有在真实的设备上运行测试。
项目
效果:
新建项目,选择 Empty Activity,在配置项目时,Minimum SDK
选择 API 16: Android 4.1 (Jelly Bean)
编辑 src\main\res\layout\activity_main.xml
,添加 FloatingActionButton
组件(第 9 ~ 18 行):
编辑 MainActivity
(第 18、21 ~ 30 行):
package com.mk;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import com.google.android.material.floatingactionbutton.FloatingActionButton;import com.google.android.material.snackbar.Snackbar;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { FloatingActionButton fab = findViewById(R.id.floatingActionButton); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); }}
参考
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月19日 04时00分48秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
#C8# UVM中的factory机制 #S8.4.1# factory机制的实现
2023-01-24
900行c语言贪吃蛇,原生js实现的贪吃蛇网页版游戏完整实例
2023-01-24
ado读取多条oracle数据,Oracle ADO数据存取
2023-01-24
android fastjson漏洞_初识Fastjson漏洞(环境搭建及漏洞复现)
2023-01-24
aspen串联反应怎么输入_如何进步提升串联谐振试验装置的稳定性
2023-01-24
c++ string取子串_Integer与String的设计哲学
2023-01-24
c++ 数组批量赋值_数组之间不能赋值?穿个马甲吧!
2023-01-24
cad模糊查询符号_mysql 正则模式和like模糊查询
2023-01-24
ctrl c 和 ctrl v 不能用了_神奇操作,原来CTRL键还能这么用
2023-01-24
cytoscape安装java_Cytoscape史上最全攻略
2023-01-24
c语言程序设计年历显示,C语言程序设计报告《万年历》.doc
2023-01-24
C语言程序设计梁海英答案,1.5 习题
2023-01-24
c语言编写单片机中断,C语言AVR单片机中断程序写法
2023-01-24
ddr2的上电顺序_S5PV210 DDR2初始化 28个步骤总结
2023-01-24
excel中最常用的30个函数_Excel玩转数据分析常用的43个函数!
2023-01-24
flink sql设置并行度_Flink 参数配置和常见参数调优
2023-01-24
go 字符串替换_Go 每日一库之 quicktemplate
2023-01-24