2017-11-02 1 views

Antwort

0

Fragment

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; 
} 

}

Dialog

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); 
} 

}

=== Ende ===

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); 

} 

}

Verwandte Themen