我有一个问题,它会显示一个自定义对话框,我的意思是单击时弹出/对话框中的片段中的按钮。 有人帮忙吗?
答案 0 :(得分:0)
<强>片段强>
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);
}
}
<强> PopupWindow 强>
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);
}
}