【发布时间】:2017-11-02 00:58:52
【问题描述】:
我在显示自定义对话框时遇到问题,我的意思是当我单击时片段中的按钮将弹出/对话框。 有人帮忙吗?
【问题讨论】:
-
发布您的代码以便我们提供帮助。
标签: java android android-fragments button popup
我在显示自定义对话框时遇到问题,我的意思是当我单击时片段中的按钮将弹出/对话框。 有人帮忙吗?
【问题讨论】:
标签: java android android-fragments button popup
片段
public abstract class BaseFragment extends Fragment {
public Context context;
public Activity activity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
//or
this.activity = (Activity) context;
}
}
对话框
public class BaseDialog extends Dialog {
public Context context;
public Activity activity;
public LayoutInflater inflater;
public Window window;
public View decorView;
private final WindowManager.LayoutParams attributes;
public BaseDialog(Context context) {
super(context);
activity = (Activity) context;
this.context = context;
inflater = LayoutInflater.from(context);
window = getWindow();
//no title
assert window != null;
window.requestFeature(Window.FEATURE_NO_TITLE);
//auto dismiss dialog
setCanceledOnTouchOutside(true);
//decorView
decorView = window.getDecorView();
attributes = getWindow().getAttributes();
}
/**
* Transparency 0.0f - 1.0f
*
* @param f
*/
public void setDimAmount(float f) {
attributes.dimAmount = f;
getWindow().setAttributes(attributes);
}
/**
* @param gravity gravity
*/
public void setGravity(int gravity) {
window.setGravity(gravity);
}
/**
* dialog animation
*
* @param resId
*/
public void setAnimation(int resId) {
window.setWindowAnimations(resId);
}
/**
* custom view
*
* @param view
* @param width
* @param height
*/
public void setContentView(View view, int width, int height) {
super.setContentView(view);
window.setLayout(width, height);
}
}
弹出窗口
public class MyPopupWindow extends PopupWindow {
public MyPopupWindow(View contentView, int width, int height) {
super(contentView, width, height);
}
@Override
public void showAsDropDown(View anchor) {
if (Build.VERSION.SDK_INT >= 24) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
setHeight(h);
}
super.showAsDropDown(anchor);
}
@Override
public void showAsDropDown(View anchor, int xoff, int yoff) {
if (Build.VERSION.SDK_INT >= 24) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
setHeight(h);
}
super.showAsDropDown(anchor, xoff, yoff);
}
}
=== 结束 ===
public class MyFragment extends BaseFragment {
public void onClick(View v) {
showDialog();
//or
showPup(v);
}
private void showDialog() {
new MyDialog(context).show();
new MyDialog(activity).show();
}
private void showPup(View v) {
MyPopupWindow popupWindow = new MyPopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setAnimationStyle(R.style.sealFragment_menu_animation);
popupWindow.showAsDropDown(view);
}
}
【讨论】: