安卓APP_ 控件(9)—— PopupWindow弹窗
生活随笔
收集整理的這篇文章主要介紹了
安卓APP_ 控件(9)—— PopupWindow弹窗
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
摘自:安卓APP_ 控件(9)—— PopupWindow彈窗
作者:丶PURSUING
發(fā)布時(shí)間: 2021-04-05 14:41:35
網(wǎng)址:https://blog.csdn.net/weixin_44742824/article/details/115405555
實(shí)現(xiàn)效果如下:
點(diǎn)擊觸發(fā)彈窗的按鈕,彈窗出現(xiàn)。彈窗里的按鈕設(shè)置了監(jiān)聽事件,當(dāng)點(diǎn)擊“上海”時(shí),彈窗不退出。點(diǎn)擊空白處彈窗退出。因?yàn)椤氨本痹O(shè)置了“dismiss”,所以點(diǎn)擊“北京”直接退出彈窗。
具體細(xì)節(jié)還是通過代碼呈現(xiàn):
MainActivity.java
public class MainActivity extends AppCompatActivity {private static final String TAG = "zhua";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void zhuaClick(View view) {/*第一個(gè)參數(shù)contentView:彈窗顯示的布局,在此之前要在layout創(chuàng)建布局.xml*/View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);//添加view//為了實(shí)現(xiàn)popupWindow中的按鈕事件,要先獲取按鈕監(jiān)聽Button btn1 = popupView.findViewById(R.id.btn1);Button btn2 = popupView.findViewById(R.id.btn2);/*1.為了方便,都用帶3參或者4參的構(gòu)造方法,否則后面還要用對(duì)象進(jìn)行設(shè)置2.根據(jù)popupWindow的一個(gè)構(gòu)造方法進(jìn)行配置,寬、高可以直接寫數(shù)字,比如300,300但是為了讓popupWindow剛好包裹住popupView,一般如下設(shè)置:跟xml中類似,都是為了內(nèi)容適應(yīng)窗口3.true為獲取焦點(diǎn),即點(diǎn)空白處可以退出popupWindow*/PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);/*設(shè)置這個(gè)背景會(huì)導(dǎo)致虛擬機(jī)崩潰?*///popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.test1));/*1.想要顯示彈窗,還要調(diào)用show.2.view指的是“彈出PopupWindow”按鈕,一般不使用偏移,直接顯示在正下方。popupWindow.showAsDropDown(view,view.getWidth(),view.getHeight());*/popupWindow.showAsDropDown(view);//彈窗直接顯示在view的正下方//設(shè)置按鈕監(jiān)聽btn1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.e(TAG, "onClick: 你是住在上海嗎" );}});btn2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.e(TAG, "onClick: 你是住在北京嗎" );//一般popupWindow按鈕事件點(diǎn)擊一個(gè)選項(xiàng)window就消失:popupWindow.dismiss();}});/*其他:設(shè)置popupwindow顯示動(dòng)畫,只需要傳入后續(xù)所學(xué)的動(dòng)畫.xml就行popupWindow.setAnimationStyle(int animationStyle);*/} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
activity_main.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:orientation="vertical"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="彈出PopupWindow "android:onClick="zhuaClick"/></LinearLayout>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
在res下的layout創(chuàng)建的: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"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btn1"android:padding="10dp"android:text="上海"android:textSize="18sp"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btn2"android:padding="10dp"android:text="北京"android:textSize="18sp"/></LinearLayout>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
總結(jié)
以上是生活随笔為你收集整理的安卓APP_ 控件(9)—— PopupWindow弹窗的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 扫雷游戏网页版_佛性扫雷 炸不炸随缘
- 下一篇: 安卓APP_ 布局(1)—— Linea