Preface:
Dialog box is often used in daily development process. If there are many dialog boxes, you can encapsulate a tool class of CustomDialog by inheriting dialog. If you use less, you can use the custom AlertDialog to implement the pop-up box.
In the process of using the custom AlertDialog, you may encounter the following problems:
1. Custom layout cannot specify size.
2. The custom layout cannot be full screen.
How to solve these two problems?
Step 1: custom layout cannot specify size
The code is as follows:
/**
* Pop up AlertDialog
*/
private void createAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View v = LayoutInflater.from(this).inflate(R.layout.dialog_recharge, null);
final Dialog dialog = builder.create();
dialog.show();
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = DensityUtil.dp2px(this, 200);
lp.height = DensityUtil.dp2px(this, 200);
window.setGravity(Gravity.CENTER);
window.setAttributes(lp);
window.setContentView(v);
}
Among them, densityutility is a dp - > PX tool class that I encapsulate. On the Internet. If I want to, I will post it directly:
/**
* Auxiliary class of common unit conversion
*/
public class DensityUtil {
private DensityUtil() {
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* dp Turn px
*/
public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
/**
* sp Turn px
*/
public static int sp2px(Context context, float spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}
/**
* px Turn dp
*/
public static float px2dp(Context context, float pxVal) {
final float scale = context.getResources().getDisplayMetrics().density;
return (pxVal / scale);
}
/**
* px Turn sp
*/
public static float px2sp(Context context, float pxVal) {
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
}
}
Step 1: custom layout cannot be full screen
In this case, in addition to setting the width and height of the custom view to the screen width and height, you also need to specify the Style of the AlertDialog.
The code is as follows
/**
* Pop up AlertDialog
*/
private void createAlertDialog1() {
AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.Dialog);//Specify stley style
View v = LayoutInflater.from(this).inflate(R.layout.dialog_recharge, null);
final Dialog dialog = builder.create();
dialog.show();
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
lp.width = display.getWidth();
lp.height=display.getHeight();
window.setAttributes(lp);
window.setContentView(v);
}
Post the style style of Dilaog:
<style name="Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowFullscreen">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
OK, it's over. If you have any questions, you can leave a message.