Android - Add a Floating Action Button

Android - Add a Floating Action Button,第1张

概述文章目录准备项目参考准备IDE:AndroidStudio4.1.1Build#AI-201.8743.12.41.6953283,builtonNovember5,2020Runtimeversion:1.8.0_242-release-1644-b01amd64VM:OpenJDK64-BitServerVMbyJetBrainss.r.oWindows1010.0AndroidVirtualDevices

文章目录准备项目参考

准备

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:\Users188\.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 行):

<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.ConstraintLayout 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"    tools:context=".MainActivity">    <com.Google.androID.material.floatingactionbutton.floatingActionbutton        androID:ID="@+ID/floatingActionbutton"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_marginBottom="16dp"        androID:clickable="true"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:srcCompat="@androID:drawable/ic_input_add" /></androIDx.constraintlayout.Widget.ConstraintLayout>

编辑 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();            }        });    }}
参考

Add a Floating Action Button

总结

以上是内存溢出为你收集整理的Android - Add a Floating Action Button全部内容,希望文章能够帮你解决Android - Add a Floating Action Button所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046530.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存