Android寬度全屏的Dialog和DialogFragment用法
阿新 • • 發佈:2019-02-08
在Android開發中我們也會常用到Dialog,我們發現,系統的Dialog很多時候不能夠滿足我們的要求,比如有預設的title等等,還有的有邊框,很難看,我們只需要載入顯示我們的佈局的樣子。那麼需要如何設定,再次記錄一下。程式碼比較簡單,高手請略過。效果圖
1.設定Style
<style name="Dialog_FullScreen">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle" >true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowFrame">@null</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:backgroundDimEnabled">true</item>
</style>
2.設定Dialog的window寬度
private void showDialog() {
Dialog dialog=new Dialog(this,R.style.Dialog_FullScreen);
dialog.setContentView(R.layout.my_dialog);
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.setCanceledOnTouchOutside(true );
dialog.setCancelable(true);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
dialog.show();
}
3.另一種方法:DialogFragment
- 重寫DialogFragment
package cn.bluemobi.dylan.fullscreendialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
/**
* Created by cookie on 2016/8/22.
*/
public class FullScreenDialog extends DialogFragment {
private View view;
private Context context;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Dialog_FullScreen);
if (null == view) {
view = LayoutInflater.from(context).inflate(R.layout.dialog_default, null);
}
builder.setView(view);
return builder.create();
}
public void setContentView(View view) {
this.view = view;
}
public View getContentView() {
return view;
}
private int gravity = Gravity.CENTER;
public void setGravity(int gravity) {
this.gravity = gravity;
}
@Override
public void onResume() {
super.onResume();
Window mWindow = getDialog().getWindow();
WindowManager.LayoutParams mLayoutParams = mWindow.getAttributes();
mLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
mLayoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
mLayoutParams.gravity =gravity;
mWindow.setAttributes(mLayoutParams);
}
}
- 用法
/**
* 顯示全屏寬度對話方塊
*/
private void showFullScreenDialog() {
FullScreenDialog bottomDialog = new FullScreenDialog();
View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.my_dialog, null);
bottomDialog.setGravity(Gravity.BOTTOM);
bottomDialog.setContentView(dialogView);
bottomDialog.show(getSupportFragmentManager(), "testdialog");
}