国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Android簡單使用PopupWindow的方法

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

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

思路

1.在res下面創(chuàng)建一個(gè)menu文件夾,并新建一個(gè)xml文件作為PoupWindow的布局文件。2.Activity中布局填充器加載菜單布局3.創(chuàng)建PopupWindow對象并設(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: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:text='選項(xiàng)一' /> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:text='選項(xiàng)二' /> <Button android: 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)出場效果內(nèi)容引用res下面的anim資源文件夾里的資源文件將動(dòng)畫綁定到popuwidow

styles資源文件

Android簡單使用PopupWindow的方法

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

anim資源文件

Android簡單使用PopupWindow的方法

Android簡單使用PopupWindow的方法

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對象 視圖對象, 寬, 高,缺一不可 * 第一種:創(chuàng)建對象的時(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); }}

Android簡單使用PopupWindow的方法

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

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 欧美国产精品亚洲精品第一区 | 成人欧美一区二区三区在线观看 | 亚洲欧美日韩视频一区 | 亚洲看片网站 | 91久久综合 | 女女互操| avtt天堂网永久资源手机版 | 熟女毛片| 国产免费成人在线视频 | 天空在线观看免费完整 | 国产日韩在线播放 | 精品一区二区三区视频在线观看 | 三级视频网站在线观看 | 国产三级在线观看a | 99久久免费观看 | 欧美综合在线观看 | 国产经典一区 | 国产一区二区久久久 | 亚洲一区二区精品推荐 | 国产亚洲精品资源一区 | 日本红怡院亚洲红怡院最新 | 日本一区二区三区国产 | 99性视频| 日韩中文字幕一在线 | 成年女人免费观看 | 亚洲国产大片 | 3级毛片 | 欧美精选欧美极品 | 国产成人一区二区视频在线观看 | 一区二区三区免费观看 | 国产成人精品久久综合 | 久久精品国产欧美日韩99热 | 日韩成人毛片高清视频免费看 | 免费在线观看的毛片 | 国产成人a一在线观看 | 99香蕉网 | 欧美精品做人一级爱免费 | 综合欧美视频一区二区三区 | 久久精品最新免费国产成人 | 韩国免费毛片 | 在线成人国产 |