av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Android簡單使用PopupWindow的方法

瀏覽:3日期:2022-09-20 16:07:06

本文實例為大家分享了Android簡單使用PopupWindow的的具體代碼,供大家參考,具體內容如下

思路

1.在res下面創(chuàng)建一個menu文件夾,并新建一個xml文件作為PoupWindow的布局文件。2.Activity中布局填充器加載菜單布局3.創(chuàng)建PopupWindow對象并設置內容以及動畫4.設置菜單布局中控件需要做的操作

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: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:text='選項一' /> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:text='選項二' /> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:text='選項三' /></LinearLayout>

定義動畫:

在styles資源文件里寫一個style繼承android:Animation,并設置進出場效果內容引用res下面的anim資源文件夾里的資源文件將動畫綁定到popuwidow

styles資源文件

Android簡單使用PopupWindow的方法

在res下面創(chuàng)建一個anim資源文件夾

anim資源文件

Android簡單使用PopupWindow的方法

Android簡單使用PopupWindow的方法

popupwindow綁定動畫 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) { //點擊事件 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對象 視圖對象, 寬, 高,缺一不可 * 第一種:創(chuàng)建對象的時候直接加參數(shù) PopupWindow popupWindow = new PopupWindow(inflate, 200, ViewGroup.LayoutParams.WRAP_CONTENT,true); * 第二種 通過setContentView,setHeight,setWidth 來設置 * 寬高可設置固定值或者ViewGroup.LayoutParams.WRAP_CONTENT **/ final PopupWindow popupWindow = new PopupWindow(inflate, 200, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setOutsideTouchable(true); //點擊彈窗外部是否取消彈窗 popupWindow.setAnimationStyle(R.style.www); //設置自定義好的動畫 //彈窗出現(xiàn)外部為陰影 WindowManager.LayoutParams attributes = getWindow().getAttributes(); attributes.alpha = 0.5f; getWindow().setAttributes(attributes); //彈窗取消監(jiān)聽 取消之后恢復陰影 popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams attributes = getWindow().getAttributes(); attributes.alpha = 1; getWindow().setAttributes(attributes); } }); //選項的點擊事件 bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); Toast.makeText(MainActivity.this, '選項一', Toast.LENGTH_SHORT).show(); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); Toast.makeText(MainActivity.this, '選項二', Toast.LENGTH_SHORT).show(); } }); bt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); Toast.makeText(MainActivity.this, '選項三', Toast.LENGTH_SHORT).show(); } }); /** * 顯示popupwidow兩種方式 * 1.howAsDropDown出現(xiàn)在下方,三個參數(shù):1綁定的控件2.x軸偏移量 3.y軸偏移量 * 2.showAtLocation自定義位置 四個參數(shù) 1綁定的控件 2.出現(xiàn)的位置 3.x軸偏移量 4.y軸偏移量 * **/ // popupWindow.showAsDropDown(bt,0,0); popupWindow.showAtLocation(bt, Gravity.CENTER, 0, 0); }}

Android簡單使用PopupWindow的方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Android
相關文章:
主站蜘蛛池模板: 日韩久久久久久 | 成人欧美一区二区三区色青冈 | 亚洲精品久久久久久首妖 | 国产成人福利在线观看 | 又黑又粗又长的欧美一区 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 国产一级片免费视频 | 91高清视频在线观看 | 欧美激情在线一区二区三区 | 最近日韩中文字幕 | www.色.com| 国产精品欧美一区二区 | 蜜桃视频一区二区三区 | 午夜视频免费在线观看 | 日韩欧美在线视频 | 久久一区 | 成人国产精品色哟哟 | 免费v片在线观看 | 天天操天天怕 | av在线免费观看网站 | 人人干人人干人人干 | 亚洲午夜精品一区二区三区他趣 | 国产美女精品视频免费观看 | 欧美一级二级在线观看 | 日本一区二区三区精品视频 | 鲁一鲁资源影视 | 中文字幕一区二区三区四区五区 | 亚洲一区二区三区视频 | 欧美成人免费在线视频 | 日韩高清中文字幕 | 欧美一区二区三区视频 | 在线日韩福利 | 麻豆精品国产91久久久久久 | 澳门永久av免费网站 | 日韩欧美中文字幕在线观看 | 国产欧美日韩一区二区三区在线观看 | 成人福利片 | 婷婷狠狠| 96国产精品久久久久aⅴ四区 | 午夜影院免费体验区 | 日韩视频在线免费观看 |