Android開發(fā)之PopupWindow實現(xiàn)彈窗效果
本文實例為大家分享了Android開發(fā)之PopupWindow實現(xiàn)彈窗的具體代碼,供大家參考,具體內(nèi)容如下
基本框架
在activity_main.xml
中設(shè)置一個按鈕,用于喚出彈窗;
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical"> ? ? <Button ? ? ? ? android:text="Popup" ? ? ? ? android:onClick="Popup" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> </LinearLayout>
再編寫一個Layout資源文件popup_view.xml
用于彈窗:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:background="@mipmap/ic_launcher" ? ? android:orientation="vertical"> ? ? <Button ? ? ? ? android:id="@+id/btn1" ? ? ? ? android:text="Button1" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> ? ? <Button ? ? ? ? android:id="@+id/btn2" ? ? ? ? android:text="Button2" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> </LinearLayout>
在MainActivity.java
中編寫按鈕的點擊事件,同樣用View popup_view=getLayoutInflater().inflate(R.layout.popup_view,null);
將上面編寫的Layout資源轉(zhuǎn)換成View,之后就可以新建一個彈窗并讓其彈出。
package com.example.mypopupwindow; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.PopupWindow; public class MainActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? } ? ? public void Popup(View view) { ? ? ? ? View popup_view=getLayoutInflater().inflate(R.layout.popup_view,null); ? ? ? ? PopupWindow window=new PopupWindow(popup_view, ViewGroup.LayoutParams.WRAP_CONTENT ? ? ? ? ? ? ? ? ,ViewGroup.LayoutParams.WRAP_CONTENT); ? ? ? ? window.showAsDropDown(view); ? ? } }
點擊按鈕就會得到彈窗:
常用方法
setContentView(View contentView)
設(shè)置彈窗的ViewshowAsDropDown(View anchor)
彈窗的位置在控件的下方showAsDropDown(View anchor,int xoff,int yoff)
相對某個控件的位置,有偏移setFocusable(boolean focusable)
設(shè)置是否獲取焦點setBackgroundDrawable(Drawable background)
設(shè)置背景dismiss()
關(guān)閉窗口setAnimationStyle(int animationStyle)
設(shè)置加載動畫(在學(xué)習(xí)了動畫后再深入)setTouchable(boolean touchable)
設(shè)置觸摸使能setOutsideTouchable(boolean touchable)
設(shè)置彈窗外面的觸摸使能
實例
將focusable設(shè)置為true,就可以通過點擊彈窗以外的地方關(guān)閉彈窗。
使用showAsDropDown(View anchor,int xoff,int yoff)
就可以讓彈窗有偏移:
使用window.setBackgroundDrawable(getResources().getDrawable(R.drawable.image));
設(shè)置彈窗的圖片背景:
對于彈窗中的兩個按鈕也可以設(shè)置監(jiān)聽,從而實現(xiàn)一些點擊事件,在點擊事件的結(jié)尾可以添加dismiss()
函數(shù)使得點擊后彈窗關(guān)閉。
package com.example.mypopupwindow; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.PopupWindow; public class MainActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? } ? ? public void Popup(View view) { ? ? ? ? View popup_view=getLayoutInflater().inflate(R.layout.popup_view,null); ? ? ? ? Button btn1=popup_view.findViewById(R.id.btn1); ? ? ? ? Button btn2=popup_view.findViewById(R.id.btn2); ? ? ? ? PopupWindow window=new PopupWindow(popup_view, ViewGroup.LayoutParams.WRAP_CONTENT ? ? ? ? ? ? ? ? ,ViewGroup.LayoutParams.WRAP_CONTENT,true); ? ? ? ? window.setBackgroundDrawable(getResources().getDrawable(R.drawable.image)); ? ? ? ? window.showAsDropDown(view); ? ? ? ? btn1.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? Log.e("ShadyPi","btn1"); ? ? ? ? ? ? ? ? window.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? btn2.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? ? ? Log.e("ShadyPi","btn2"); ? ? ? ? ? ? ? ? window.dismiss(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android camera yuv幀水平翻轉(zhuǎn)實例
今天小編就為大家分享一篇android camera yuv幀水平翻轉(zhuǎn)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Android中可以作為Log開關(guān)的一些操作及安全性詳解
Android的調(diào)試好伙伴Log在調(diào)試時非常有用,基本可以看Log而無需單點調(diào)試,尤其對實時大數(shù)據(jù)量的設(shè)備調(diào)試尤其有效,下面這篇文章就來給大家詳細(xì)介紹關(guān)于Android中可以作為Log開關(guān)的一些操作及安全性的相關(guān)資料,需要的朋友可以參考下。2017-12-12android ViewPager實現(xiàn)滑動翻頁效果實例代碼
本篇文章主要介紹了android ViewPager實現(xiàn)滑動翻頁效果實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03Android 帶箭頭的指引tipLayout實現(xiàn)示例代碼
本篇文章主要介紹了Android 帶箭頭的指引tipLayout實現(xiàn)示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01android換膚功能 如何動態(tài)獲取控件中背景圖片的資源id?
這篇文章主要為大家詳細(xì)介紹了android換膚功能中如何動態(tài)獲取控件中背景圖片的資源id? ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08Android三種方式實現(xiàn)ProgressBar自定義圓形進度條
這篇文章主要介紹了Android三種方式實現(xiàn)ProgressBar自定義圓形進度條的相關(guān)資料,需要的朋友可以參考下2016-03-03Android 中RecycleView實現(xiàn)item的點擊事件
這篇文章主要介紹了Android 中RecycleView實現(xiàn)item的點擊事件的相關(guān)資料,需要的朋友可以參考下2017-03-03