android实现弹窗的方法,Android中的各种弹窗实现

在 Android 中实现弹出层方式很多,下面介绍几种常用的

1.通过FrameLayout模拟弹出层

优点:实现简单,布局也比较好写

缺点:位置在上下左右比较好,其他复杂的布局要写大量的点击事件来阻止点击向下传递

比如实现下面的效果:

bdeeee288e9a28125e302ff796daa720.png

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/but01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click"/>

android:id="@+id/popouter"

android:visibility="gone"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="0dp"

android:background="#AAA"

android:alpha="0.5"

android:id="@+id/popinner1"

android:layout_weight="5">

android:id="@+id/popinner2"

android:gravity="center"

android:layout_width="match_parent"

android:layout_height="0dp"

android:bac

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用程序实现弹窗,可以按照以下步骤操作: 1. 在布局文件添加一个按钮和一个布局文件,用于显示弹窗内容。 2. 在Java代码,获取按钮和弹窗布局文件的实例,并设置按钮的点击事件。 3. 在按钮的点击事件,创建一个弹窗实例,并设置弹窗的内容和样式。 4. 显示弹窗。 以下是一个简单的示例代码(假设我们的布局文件名为activity_main.xml,弹窗布局文件名为popup_window.xml): 1. 在activity_main.xml添加一个按钮和一个布局文件: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_open_popup" android:text="Open Popup" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:id="@+id/popup_layout" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:background="#ffffff"> <TextView android:text="This is a popup window" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_close_popup" android:text="Close" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> ``` 2. 在MainActivity.java,获取按钮和弹窗布局文件的实例,并设置按钮的点击事件: ``` public class MainActivity extends AppCompatActivity { private Button btnOpenPopup; private LinearLayout popupLayout; private PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnOpenPopup = findViewById(R.id.btn_open_popup); popupLayout = findViewById(R.id.popup_layout); btnOpenPopup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openPopupWindow(); } }); } // TODO: 实现openPopupWindow()方法 } ``` 3. 在openPopupWindow()方法,创建一个弹窗实例,并设置弹窗的内容和样式: ``` private void openPopupWindow() { popupWindow = new PopupWindow(this); popupWindow.setContentView(popupLayout); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); Button btnClosePopup = popupLayout.findViewById(R.id.btn_close_popup); btnClosePopup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); } ``` 4. 最后,在openPopupWindow()方法,显示弹窗: ``` private void openPopupWindow() { popupWindow = new PopupWindow(this); popupWindow.setContentView(popupLayout); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); Button btnClosePopup = popupLayout.findViewById(R.id.btn_close_popup); btnClosePopup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); popupWindow.showAtLocation(btnOpenPopup, Gravity.CENTER, 0, 0); } ``` 现在,当用户点击按钮时,弹窗将显示在屏幕央。用户可以点击弹窗的“关闭”按钮来关闭弹窗
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值