运城住房和建设局网站,网站开发销售话术,灯饰网站建设,学做饺子馅上那个网站一、常用方法#xff1a;
1、设置显示的位置
// 一个参数
popupWindow.showAsDropDown(v);
//参数1: popupWindow关联的view
// 参数2和3#xff1a;相对于关联控件的偏移量popupWindow.showAsDropDown(View anchor, int xoff, int yoff)2、是否会获取焦点
popupWindow.se…一、常用方法
1、设置显示的位置
// 一个参数
popupWindow.showAsDropDown(v);
//参数1: popupWindow关联的view
// 参数2和3相对于关联控件的偏移量popupWindow.showAsDropDown(View anchor, int xoff, int yoff)2、是否会获取焦点
popupWindow.setFocusable(true);3、设置背景 // popupWindow添加背景色
popupWindow.setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.img1,getTheme()));4、关闭
popupWindow.dismiss();5、设置加载动画 popupWindow.setAnimationStyle(R.anim.alpha); //设置动画6、设置触摸使能和popupwindow外部的触摸使能 popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);二、实例
1、新建一个popupwindow要显示使用的xml文件popupview.xml
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentxmlns:apphttp://schemas.android.com/apk/res-autoandroidx.appcompat.widget.AppCompatButtonandroid:idid/mBtn1android:layout_width100dpandroid:layout_height50dpandroid:text测试1app:layout_constraintLeft_toLeftOfparentapp:layout_constraintTop_toTopOfparent/androidx.appcompat.widget.AppCompatButtonandroid:idid/mBtn2android:layout_width100dpandroid:layout_height50dpandroid:text测试2android:layout_marginTop10dpapp:layout_constraintLeft_toLeftOfparentapp:layout_constraintTop_toBottomOfid/mBtn1/
/androidx.constraintlayout.widget.ConstraintLayout2、在activity中使用 Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_tween_anim);Button buttonfindViewById(R.id.btn);button.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {popupClick(v);}});}private void popupClick(View v) {
// 添加对应的viewView popupView getLayoutInflater().inflate(R.layout.popup_view, null);Button mBtn1popupView.findViewById(R.id.mBtn1);Button mBtn2popupView.findViewById(R.id.mBtn2);
// ViewGroup.LayoutParams.WRAP_CONTENT: popupWindow包裹popupView
// 第四个参数为true点击空白处popupWindow关闭PopupWindow popupWindow new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);// popupWindow添加背景色popupWindow.setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.img1,getTheme()));popupWindow.setAnimationStyle(R.anim.alpha); //设置动画
// popupWindow.showAsDropDown(v);
// 参数2和参数3是设置偏移量popupWindow.showAsDropDown(v,popupView.getWidth(),-popupView.getHeight());mBtn1.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {popupWindow.dismiss();}});mBtn2.setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {popupWindow.dismiss();}});}