基于Android6.0實(shí)現(xiàn)彈出Window提示框
在項(xiàng)目中經(jīng)常會(huì)需要應(yīng)用彈出一些自定義的窗口,這時(shí)候Android自帶的系統(tǒng)Dialog就無法滿足我們的需求了,為了滿足項(xiàng)目需求,我們可以使用Window來滿足這一需求。
首先我們新建一個(gè)項(xiàng)目,來到MainActivity的布局文件,在這里新建一個(gè)按鈕用于彈出Window
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_window" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="open window"/> </LinearLayout>
接著我們完成window的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:paddingLeft="10dp" android:background="@android:color/darker_gray" android:orientation="vertical" android:layout_width="300dp" android:layout_height="200dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="發(fā)件人——13110203356" android:textSize="18sp" android:textColor="@color/colorPrimary"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginTop="5dp" android:text="這里是內(nèi)容" android:textSize="15sp" android:textColor="@color/colorAccent"/> <Button android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="按鈕" android:layout_gravity="right"/> </LinearLayout> </LinearLayout>
這個(gè)布局實(shí)際上類似于短信提示框,效果如下
之后在MainActivity中完成彈出Window的邏輯,首先我們要知道在Android6.0之前,使用Window只需要聲明權(quán)限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>即可,但是從Android6.0開始,應(yīng)用要想彈出一個(gè)懸浮在任意App上的Window的話需要用戶手動(dòng)為這個(gè)應(yīng)用設(shè)置overlays權(quán)限,這個(gè)權(quán)限就連運(yùn)行時(shí)權(quán)限也無法拿到,必須要用戶前往手機(jī)的權(quán)限界面為應(yīng)用設(shè)置該權(quán)限,因此在彈出window之前我們首先要進(jìn)行一個(gè)邏輯判斷,判斷用戶是否已經(jīng)獲取過overlays權(quán)限,如果獲取了權(quán)限就直接彈出window,若沒有獲取過overlays權(quán)限則會(huì)自動(dòng)將界面跳轉(zhuǎn)到權(quán)限設(shè)置界面,讓用戶決定是否提供該權(quán)限,最后,代碼如下
public class MainActivity extends AppCompatActivity { private Button btn_window; private Button btn_send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_window= (Button) findViewById(R.id.btn_window); btn_window.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) { //版本大于6.0則需要判斷是否獲取了overlays權(quán)限 if (!Settings.canDrawOverlays(MainActivity.this)) { startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:"+getPackageName())),1); } else { openWindow(); } } } }); } private void openWindow() { //獲取WindowManager實(shí)例 final WindowManager windowManager= (WindowManager) getSystemService(Context.WINDOW_SERVICE); //獲取窗口布局參數(shù)實(shí)例 WindowManager.LayoutParams params=new WindowManager.LayoutParams(); //設(shè)置窗口布局參數(shù)屬性 params.width= WindowManager.LayoutParams.MATCH_PARENT; params.height=WindowManager.LayoutParams.MATCH_PARENT; //設(shè)置window的顯示特性 params.flags=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; //設(shè)置窗口半透明 params.format= PixelFormat.TRANSLUCENT; //設(shè)置窗口類型 params.type=WindowManager.LayoutParams.TYPE_PHONE; //獲取窗口布局實(shí)例 final View windowView=View.inflate(this,R.layout.window,null); //獲取窗口布局中的按鈕實(shí)例 btn_send= (Button) windowView.findViewById(R.id.btn_send); //設(shè)置點(diǎn)擊事件 btn_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { windowManager.removeView(windowView); } }); //顯示窗口 windowManager.addView(windowView,params); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: { if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) { if (Settings.canDrawOverlays(this)) { //若用戶開啟了overlay權(quán)限,則打開window openWindow(); } else { Toast.makeText(this,"不開啟overlay權(quán)限",Toast.LENGTH_SHORT).show(); } } break; } } } }
最后的效果如下,當(dāng)然別忘了在Android6.0后也是需要在Manifest文件中聲明權(quán)限的
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android 彈出提示框的使用(圖文實(shí)例)
- Android使用Toast顯示消息提示框
- Android仿QQ、微信聊天界面長按提示框效果
- Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
- Android仿IOS自定義AlertDialog提示框
- Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡單應(yīng)用示例
- android實(shí)現(xiàn)彈出提示框
- Android超實(shí)用的Toast提示框優(yōu)化分享
- Android中仿IOS提示框的實(shí)現(xiàn)方法
- Android模擬美團(tuán)客戶端進(jìn)度提示框
相關(guān)文章
Android 新聞界面模擬ListView和ViewPager的應(yīng)用
本文主要介紹 Android ListView和ViewPager的應(yīng)用,這里模擬了新聞界面及實(shí)現(xiàn)示例代碼,有需要的小伙伴可以參考下2016-09-09Android 4.4.2 橫屏應(yīng)用隱藏狀態(tài)欄和底部虛擬鍵的方法
這篇文章主要介紹了Android 4.4.2 橫屏應(yīng)用隱藏狀態(tài)欄和底部虛擬鍵的方法,需要的朋友可以參考下2017-01-01Android手機(jī)鬧鐘服務(wù)AlarmManagerk開發(fā)案例
這篇文章主要為大家詳細(xì)介紹了Android手機(jī)鬧鐘服務(wù)AlarmManagerk開發(fā)案例的資料,需要的朋友可以參考下2016-05-05Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕的具體代碼,感興趣的小伙伴們可以參考一下2016-05-05android通過jxl讀excel存入sqlite3數(shù)據(jù)庫
本文主要介紹了android通過jxl去讀excel的內(nèi)容,然后存入sqlite3數(shù)據(jù)庫表,需要用到j(luò)xl的jar包和sqlite 的jar包,圖片是excel的數(shù)據(jù)格式,需要的朋友可以參考下2014-03-03Android中home鍵和back鍵區(qū)別實(shí)例分析
這篇文章主要介紹了Android中home鍵和back鍵區(qū)別,以實(shí)例形式較為詳細(xì)的分析并總結(jié)了home鍵和back鍵區(qū)別及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09Android編程之canvas繪制各種圖形(點(diǎn),直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
這篇文章主要介紹了Android編程之canvas繪制各種圖形的方法,涉及Android使用Canvas類中常用繪圖方法的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12