Android超簡單懸浮窗使用教程
完全自定義懸浮窗,保證100%學會的超簡單懸浮窗
先看看效果圖:
圖1 圖2 圖3
圖1只需要31行代碼即可完成。
我們來看看這些都是如何實現的
使用前需要依賴庫:
第一步:將以下存儲庫將其添加到根構建中。
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
第二步:添加依賴關系 build.gradle(Module:app)
(注意:當前演示的版本號是1.3.1.3版本,今后會有新的版本發(fā)布,歡迎使用最新版)
dependencies { ... implementation 'com.github.1079374315:GSLS_Tool:v1.3.1.3' }
依賴詳細教程:
第一步:自定義的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 必須要將對話框大小設置出來,解決設置最外層寬高無效的問題 --> <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="簡易自定義對話框\n支持返回數據\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);//設置可拖動 } @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);//啟動懸浮窗 } }); } }
總結:熟悉GT庫的是不是感覺特別熟悉,沒錯,GT內的Activity、Fragment、DialogFragment、FloatingWindow 的使用方法與結構都是一模一樣的,也就是說只要你學會了其中一個,那就等同于其他的你都學會了。
糖豆:如果想要圖2、圖3的源碼,請直接下載最新GT庫,GT庫中的 util 目錄中就是源碼。 啟動GT模擬手機版懸浮窗的代碼如下:
點個關注點個贊唄(〃'▽'〃),關注博主最新發(fā)布庫:GitHub - 1079374315/GT
到此這篇關于Android超簡單懸浮窗使用教程的文章就介紹到這了,更多相關Android懸浮窗內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android中ViewPager1和ViewPager2的使用教程
這篇文章主要介紹了Android中ViewPager1和ViewPager2的使用,效果圖是結合BottomNavigationView+ViewPager一起使用的,具體實例代碼跟隨小編一起看看吧2021-10-10Android百度定位導航之基于百度地圖移動獲取位置和自動定位
項目需求是這樣的,首先定位我當前的起始位置,并跟隨移動不斷自動定位我的當前位置,下面通過本文給大家介紹android百度定位導航之基于百度地圖移動獲取位置和自動定位,需要的朋友參考下2016-01-01android+json+php+mysql實現用戶反饋功能方法解析
相信每個項目都會有用戶反饋建議等功能,這個實現的方法很多,下面是我實現的方法,供大家交流2012-11-11Android事件分發(fā)機制(上) ViewGroup的事件分發(fā)
這篇文章主要為大家詳細介紹了Android ViewGroup的事件分發(fā)機制上篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01