Android超簡(jiǎn)單懸浮窗使用教程
完全自定義懸浮窗,保證100%學(xué)會(huì)的超簡(jiǎn)單懸浮窗
先看看效果圖:
圖1 圖2 圖3
圖1只需要31行代碼即可完成。
我們來看看這些都是如何實(shí)現(xiàn)的
使用前需要依賴庫(kù):
第一步:將以下存儲(chǔ)庫(kù)將其添加到根構(gòu)建中。
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
第二步:添加依賴關(guān)系 build.gradle(Module:app)
(注意:當(dāng)前演示的版本號(hào)是1.3.1.3版本,今后會(huì)有新的版本發(fā)布,歡迎使用最新版)
dependencies { ... implementation 'com.github.1079374315:GSLS_Tool:v1.3.1.3' }
依賴詳細(xì)教程:
第一步:自定義的xml布局 demo_floating_window
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!-- ConstraintLayout 必須要將對(duì)話框大小設(shè)置出來,解決設(shè)置最外層寬高無效的問題 --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/view_bg" android:layout_width="300dp" android:layout_height="200dp" android:background="#5B77D5FF" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:id="@+id/tv_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="×" android:textColor="#99FFFFFF" android:textSize="28sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tv_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#8D03B1FF" android:padding="10dp" android:text="簡(jiǎn)易自定義對(duì)話框\n支持返回?cái)?shù)據(jù)\n支持監(jiān)聽返回鍵\n用法與 Fragment 毫無差異\n" android:textColor="#A4FFFFFF" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/btn_ok" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv_back" /> <Button android:id="@+id/btn_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="好的" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/btn_cancel" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" app:layout_constraintBottom_toBottomOf="@+id/btn_ok" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/btn_ok" /> </androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout>
第二步:加載懸浮窗布局
//加載布局 @GT.Annotations.GT_AnnotationDialogFragment(R.layout.demo_floating_window) public class DemoFloatingWindow extends GT.GT_FloatingWindow.AnnotationFloatingWindow { @Override protected void initView(View view) { super.initView(view); setDrag(true);//設(shè)置可拖動(dòng) } @GT.Annotations.GT_Click({R.id.btn_ok, R.id.tv_back, R.id.btn_cancel}) public void onClick(View view) { switch (view.getId()) { case R.id.btn_ok: GT.toast("單擊了ok"); break; case R.id.tv_back: case R.id.btn_cancel: finish(); break; } } }
第三步:使用懸浮窗
//加載布局 @GT.Annotations.GT_AnnotationActivity(R.layout.activity_main) public class MainActivity extends GT.GT_Activity.AnnotationActivity { @Override protected void initView(Bundle savedInstanceState) { super.initView(savedInstanceState); findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startFloatingWindow(DemoFloatingWindow.class);//啟動(dòng)懸浮窗 } }); } }
總結(jié):熟悉GT庫(kù)的是不是感覺特別熟悉,沒錯(cuò),GT內(nèi)的Activity、Fragment、DialogFragment、FloatingWindow 的使用方法與結(jié)構(gòu)都是一模一樣的,也就是說只要你學(xué)會(huì)了其中一個(gè),那就等同于其他的你都學(xué)會(huì)了。
糖豆:如果想要圖2、圖3的源碼,請(qǐng)直接下載最新GT庫(kù),GT庫(kù)中的 util 目錄中就是源碼。 啟動(dòng)GT模擬手機(jī)版懸浮窗的代碼如下:
點(diǎn)個(gè)關(guān)注點(diǎn)個(gè)贊唄(〃'▽'〃),關(guān)注博主最新發(fā)布庫(kù):GitHub - 1079374315/GT
到此這篇關(guān)于Android超簡(jiǎn)單懸浮窗使用教程的文章就介紹到這了,更多相關(guān)Android懸浮窗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Android實(shí)現(xiàn)跨頁面懸浮窗效果
- Android懸浮窗的實(shí)現(xiàn)步驟
- Android開發(fā)懸浮窗踩坑解決
- Android 無障礙全局懸浮窗實(shí)現(xiàn)示例
- Android實(shí)現(xiàn)懸浮窗效果
- Android應(yīng)用內(nèi)懸浮窗Activity的簡(jiǎn)單實(shí)現(xiàn)
- Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法實(shí)例
- Android創(chuàng)建懸浮窗的完整步驟
- Android 懸浮窗開發(fā)示例((動(dòng)態(tài)權(quán)限請(qǐng)求 | 前臺(tái)服務(wù)和通知 | 懸浮窗創(chuàng)建 )
相關(guān)文章
Android中ViewPager1和ViewPager2的使用教程
這篇文章主要介紹了Android中ViewPager1和ViewPager2的使用,效果圖是結(jié)合BottomNavigationView+ViewPager一起使用的,具體實(shí)例代碼跟隨小編一起看看吧2021-10-10Android實(shí)現(xiàn)內(nèi)存中數(shù)據(jù)保存到sdcard的方法
這篇文章主要介紹了Android實(shí)現(xiàn)內(nèi)存中數(shù)據(jù)保存到sdcard的方法,涉及Android的文件讀寫與I/O操作相關(guān)技巧,需要的朋友可以參考下2016-01-01Android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位
項(xiàng)目需求是這樣的,首先定位我當(dāng)前的起始位置,并跟隨移動(dòng)不斷自動(dòng)定位我的當(dāng)前位置,下面通過本文給大家介紹android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位,需要的朋友參考下2016-01-01一文帶你了解Android中的網(wǎng)絡(luò)請(qǐng)求
安卓開發(fā)網(wǎng)絡(luò)請(qǐng)求可謂是安卓開發(fā)的靈魂,如果你不會(huì)網(wǎng)絡(luò)請(qǐng)求,那么你開發(fā)的應(yīng)用軟件就是一具沒有靈魂的枯骨。本文主要為大家介紹的是Android的網(wǎng)絡(luò)請(qǐng)求,感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-11-11Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05android+json+php+mysql實(shí)現(xiàn)用戶反饋功能方法解析
相信每個(gè)項(xiàng)目都會(huì)有用戶反饋建議等功能,這個(gè)實(shí)現(xiàn)的方法很多,下面是我實(shí)現(xiàn)的方法,供大家交流2012-11-11Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面,主要采用SharedPreferences來保存用戶數(shù)據(jù),感興趣的小伙伴們可以參考一下2016-05-05Android事件分發(fā)機(jī)制(上) ViewGroup的事件分發(fā)
這篇文章主要為大家詳細(xì)介紹了Android ViewGroup的事件分發(fā)機(jī)制上篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01