Android簡(jiǎn)單使用PopupWindow的方法
本文實(shí)例為大家分享了Android簡(jiǎn)單使用PopupWindow的的具體代碼,供大家參考,具體內(nèi)容如下
思路
1.在res下面創(chuàng)建一個(gè)menu文件夾,并新建一個(gè)xml文件作為PoupWindow的布局文件。
2.Activity中布局填充器加載菜單布局
3.創(chuàng)建PopupWindow對(duì)象并設(shè)置內(nèi)容以及動(dòng)畫
4.設(shè)置菜單布局中控件需要做的操作
menu菜單布局:
<?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="#eaeaea" android:orientation="vertical"> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="選項(xiàng)一" /> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="選項(xiàng)二" /> <Button android:id="@+id/bt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="選項(xiàng)三" /> </LinearLayout>
定義動(dòng)畫:
在styles資源文件里寫一個(gè)style繼承android:Animation,并設(shè)置進(jìn)出場(chǎng)效果
內(nèi)容引用res下面的anim資源文件夾里的資源文件
將動(dòng)畫綁定到popuwidow
styles資源文件
在res下面創(chuàng)建一個(gè)anim資源文件夾
anim資源文件
popupwindow綁定動(dòng)畫 popupWindow.setAnimationStyle(R.style.www);
Activity
public class MainActivity extends AppCompatActivity { private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = findViewById(R.id.bt); } public void click(View view) { //點(diǎn)擊事件 View inflate = LayoutInflater.from(this).inflate(R.layout.menu, null); //布局加載器加載菜單布局 Button bt1 = inflate.findViewById(R.id.bt1); Button bt2 = inflate.findViewById(R.id.bt2); Button bt3 = inflate.findViewById(R.id.bt3); /** * 創(chuàng)建PopupWindow對(duì)象 視圖對(duì)象, 寬, 高,缺一不可 * 第一種:創(chuàng)建對(duì)象的時(shí)候直接加參數(shù) PopupWindow popupWindow = new PopupWindow(inflate, 200, ViewGroup.LayoutParams.WRAP_CONTENT,true); * 第二種 通過setContentView,setHeight,setWidth 來設(shè)置 * 寬高可設(shè)置固定值或者ViewGroup.LayoutParams.WRAP_CONTENT **/ final PopupWindow popupWindow = new PopupWindow(inflate, 200, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setOutsideTouchable(true); //點(diǎn)擊彈窗外部是否取消彈窗 popupWindow.setAnimationStyle(R.style.www); //設(shè)置自定義好的動(dòng)畫 //彈窗出現(xiàn)外部為陰影 WindowManager.LayoutParams attributes = getWindow().getAttributes(); attributes.alpha = 0.5f; getWindow().setAttributes(attributes); //彈窗取消監(jiān)聽 取消之后恢復(fù)陰影 popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams attributes = getWindow().getAttributes(); attributes.alpha = 1; getWindow().setAttributes(attributes); } }); //選項(xiàng)的點(diǎn)擊事件 bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); Toast.makeText(MainActivity.this, "選項(xiàng)一", Toast.LENGTH_SHORT).show(); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); Toast.makeText(MainActivity.this, "選項(xiàng)二", Toast.LENGTH_SHORT).show(); } }); bt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); Toast.makeText(MainActivity.this, "選項(xiàng)三", Toast.LENGTH_SHORT).show(); } }); /** * 顯示popupwidow兩種方式 * 1.howAsDropDown出現(xiàn)在下方,三個(gè)參數(shù):1綁定的控件2.x軸偏移量 3.y軸偏移量 * 2.showAtLocation自定義位置 四個(gè)參數(shù) 1綁定的控件 2.出現(xiàn)的位置 3.x軸偏移量 4.y軸偏移量 * **/ // popupWindow.showAsDropDown(bt,0,0); popupWindow.showAtLocation(bt, Gravity.CENTER, 0, 0); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中查看USB連接的外接設(shè)備信息的代碼實(shí)例
這篇文章主要介紹了Android中查看USB連接的外接設(shè)備信息的代碼實(shí)例,需要的朋友可以參考下2014-04-04Android判斷手機(jī)是否是小米MIUI系統(tǒng)的方法
這篇文章主要介紹了Android判斷手機(jī)是否是小米MIUI系統(tǒng)的方法的相關(guān)資料,需要的朋友可以參考下2016-02-02Android 開發(fā)延時(shí)調(diào)用的幾種方法
本篇文章主要介紹Android 延遲, 在Android 程序開發(fā)過程中經(jīng)常會(huì)遇到程序推遲時(shí)間執(zhí)行,這里整理了幾種方法,以便Android 開發(fā)的朋友參考2016-07-07Android Studio無法改變Button背景顏色解決辦法
今天我來和大家探討一個(gè)在Android開發(fā)中常見但可能讓初學(xué)者感到困惑的問題,如何在Android Studio中改變Button的背景顏色,這個(gè)問題看似簡(jiǎn)單,但實(shí)際操作中可能會(huì)遇到一些意想不到的挑戰(zhàn),接下來,我將從多個(gè)角度為大家提供解決方案,需要的朋友可以參考下2024-05-05Android實(shí)現(xiàn)微信朋友圈發(fā)本地視頻功能
這篇文章主要介紹了Android實(shí)現(xiàn)微信朋友圈發(fā)本地視頻功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11andoid打包短信發(fā)送到gmail郵箱實(shí)現(xiàn)代碼
andriod短信整合備份發(fā)送到gmail郵箱,需要在andoid手機(jī)配置好gmail郵箱,下面是具體的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈2013-06-06Input系統(tǒng)之InputReader處理按鍵事件詳解
這篇文章主要為大家介紹了Input系統(tǒng)之InputReader處理按鍵事件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android Studio下的APP目錄結(jié)構(gòu)詳解
這篇文章主要介紹了AndroidStudio下的APP目錄結(jié)構(gòu),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05