Android宽度全屏的Dialog和DialogFragment用法

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

4.GitHub:https://github.com/linglongxin24/FullScreenDialog

你可能感兴趣的:(android,dialog,宽度,全屏,DialogFrag)