setContentView(R.layout.activity_main); : 设置popupWindow显示View
popupWindow.showAsDropDown(view); : 相对某个控件的位置,无偏移
popupWindow.showAsDropDown(view,100dp,100dp); : 相对某个控件的位置,有偏移
setFocusable(true) : 设置是否获取焦点
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ceshi)); : 设置背景
popupWindow.dismiss() : 关闭弹窗
popupWindow.setAnimationStyle(int ) : 设置加载动画
popupWindow.setTouchable(true) : 设置触摸功能
popupWindow.setOutSideTouchable(true) : 设置外部触摸功能
前端示例activity_main.xml
<?xml version=”1.0” encoding=”utf-8”?>
android:layout_height=”match_parent”
xmlns:app=”http://schemas.android.com/apk/res-auto“
android:orientation=”vertical”
xmlns:android=”http://schemas.android.com/apk/res/android">
<Button<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="设置"<br /> android:onClick="liu"<br /> /><br /></LinearLayout>
前端示例popup_view.xml
<?xml version=”1.0” encoding=”utf-8”?>
android:layout_height=”match_parent”
xmlns:app=”http://schemas.android.com/apk/res-auto“
android:orientation=”vertical”
android:background=”@mipmap/ic_launcher”
xmlns:android=”http://schemas.android.com/apk/res/android">
<Button<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:id="@+id/btn1"<br /> android:padding="5dp"<br /> android:text="移库"<br /> android:textSize="18sp"<br /> /><br /> <Button<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:id="@+id/btn2"<br /> android:padding="5dp"<br /> android:text="上架"<br /> android:textSize="18sp"<br /> /><br /></LinearLayout>
后端代码示例
package com.example.popupWindow;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
public class MainActivity extends AppCompatActivity {
@Override<br /> protected void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout._activity_main_);<br /> }
public void liu(View view) {<br /> View inflate = getLayoutInflater().inflate(R.layout._popup_view_, null);<br /> Button btn1 = inflate.findViewById(R.id._btn1_);<br /> Button btn2 = inflate.findViewById(R.id._btn2_);<br /> PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams._WRAP_CONTENT_,ViewGroup.LayoutParams._WRAP_CONTENT_,true);<br /> popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable._ceshi_));<br /> popupWindow.showAsDropDown(view);<br /> btn1.setOnClickListener(new View.OnClickListener() {<br /> @Override<br /> public void onClick(View view) {<br /> Log._e_("beleth","是否需要移库");<br /> popupWindow.dismiss();<br /> }<br /> });<br /> btn2.setOnClickListener(new View.OnClickListener() {<br /> @Override<br /> public void onClick(View view) {<br /> Log._e_("beleth","是否需要拣货");<br /> popupWindow.dismiss();<br /> }<br /> });<br /> }<br />}