在Android开发中,PopupWindow与AlertDialog是我们在实际开发中可能用到的,其功能相当于弹窗实现相应的需求。AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加的灵活。
1.setContentView(View contentView)设置PopupWindow显示的View。
2.showAsDropDown(View anchor)相对某个控件的位置(正左下方),无偏移
3.showAsDropDown(View anchor,int xoff,int yoff)相对某个控件的位置,有偏移
4.setFocusable(boolean focusable)设置是否获取焦点
5.setBackgroundDrawable(Drawable background)设置背景
6.dismiss()关闭弹窗
7.setAnimationStyle(int animationStyle)设置加载动画
8.setTouchable(boolean touchable)设置触摸使能
9.setOutsideTouchable(boolean touchable)设置PopupWindow外面的触摸使能
package com.njyd.mypopupwindow;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void leoClick(View view) {
final View popupView = getLayoutInflater().inflate(R.layout.popup_view,null);//设置PopupView的来源
Spinner product = popupView.findViewById(R.id.product);
Spinner component = popupView.findViewById(R.id.component);
Spinner point = popupView.findViewById(R.id.point);
final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,true);//设置PopupWindow的PopupView、长、宽,focusable设置是否获取焦点
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon));//设置背景
popupWindow.showAsDropDown(view);//显示在按钮的下方
product.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String str = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
component.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String str = adapterView.getItemAtPosition(i).toString();
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
point.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String str = adapterView.getItemAtPosition(i).toString();
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设备"
android:textSize="15sp"/>
<Spinner
android:id="@+id/product"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="3.5"
android:entries="@array/product" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="部件"
android:textSize="15sp"/>
<Spinner
android:id="@+id/component"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.2"
android:entries="@array/component"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="测点"
android:textSize="15sp"/>
<Spinner
android:id="@+id/point"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.5"
android:entries="@array/point"
/>
</LinearLayout>