NormalDialogFragmentDemo【普通頁面的DialogFragment】

版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!android

前言

該Demo主要演示DialogFragment做爲普通頁面,顯示全屏和狀態欄下方的效果以及動畫效果。git

效果圖

 

代碼分析

    @Override
    public void onStart() {
        super.onStart();

        if(mTag.equals(TAG_FULLSCREEN)){//全屏顯示
            //設置對話框的寬高,必須在onStart中
            Window window = this.getDialog().getWindow();
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//全屏(蓋住狀態欄)
            window.setGravity(Gravity.BOTTOM);//設置在底部
            //打開的動畫效果--從底部向上
            window.setWindowAnimations(R.style.bottomsheetdialog_animation);
        }else{
            //從個人場景列表界面中設置按鈕打開的
            //設置對話框的寬高,必須在onStart中
            DisplayMetrics metrics = new DisplayMetrics();
            this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
            Window window = this.getDialog().getWindow();
            if(getStatusBarHeight(mContext) <= 96){
                window.setLayout(metrics.widthPixels, metrics.heightPixels - getStatusBarHeight(mContext));
            }else{
                window.setLayout(metrics.widthPixels, this.getDialog().getWindow().getAttributes().height);//適配紅米6pro
            }
            window.setGravity(Gravity.BOTTOM);//設置在底部
            //打開的動畫效果--從底部向上
            window.setWindowAnimations(R.style.bottomsheetdialog_animation);
        }
    }

使用步驟

1、項目組織結構圖

注意事項:github

一、  導入類文件後須要change包名以及從新import R文件路徑web

二、  Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋app

2、導入步驟

由於這個demo不是封裝某個功能,因此這裏只是簡單說一下關鍵的文件。ide

PreviewDialog文件

package com.why.project.normaldialogfragmentdemo.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.why.project.normaldialogfragmentdemo.R;

/**
 * Created by HaiyuKing
 * Used
 */

public class PreviewDialog extends BaseDialogFragment{
    private static final String TAG = PreviewDialog.class.getSimpleName();

    public static final String TAG_FULLSCREEN = "fullScreen";//全屏
    public static final String TAG_BELOWSTATUEBAR = "belowStatusBar";//狀態欄下方

    /**View實例*/
    private View myView;
    /**context實例*/
    private Context mContext;
    /**標記:用來表明是從哪一個界面打開的這個對話框*/
    private String mTag;

    private WebView mWebView;

    public static PreviewDialog getInstance(Context mContext, Bundle bundle)
    {
        PreviewDialog previewDialog = new PreviewDialog();
        previewDialog.mContext = mContext;
        return previewDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_NoTitleBar_Fullscreen);//全屏(在狀態欄底下)
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));//設置背景爲透明,而且沒有標題
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題欄
        //設置窗體全屏
        getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        myView = inflater.inflate(R.layout.dialog_preview, container, false);
        /*this.getDialog().setOnKeyListener(new DialogInterface.OnKeyListener()
        {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event){
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    LogUtil.w(TAG, "onKey");
                    dismiss();
                    return true; // return true是中斷事件,那麼下面的就接受不到按鍵信息了
                }else
                {
                    return false; //在return false的時候 纔會事件繼續向下傳遞。
                }
            }
        });*/
        return myView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        //初始化控件以及設置
        initView();
        //初始化數據
        initDatas();
        initEvents();
    }

    /**
     * 設置寬度和高度值,以及打開的動畫效果
     */
    @Override
    public void onStart() {
        super.onStart();

        if(mTag.equals(TAG_FULLSCREEN)){//全屏顯示 //設置對話框的寬高,必須在onStart中
            Window window = this.getDialog().getWindow(); window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//全屏(蓋住狀態欄)
            window.setGravity(Gravity.BOTTOM);//設置在底部 //打開的動畫效果--從底部向上
 window.setWindowAnimations(R.style.bottomsheetdialog_animation); }else{ //從個人場景列表界面中設置按鈕打開的 //設置對話框的寬高,必須在onStart中
            DisplayMetrics metrics = new DisplayMetrics(); this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); Window window = this.getDialog().getWindow(); if(getStatusBarHeight(mContext) <= 96){ window.setLayout(metrics.widthPixels, metrics.heightPixels - getStatusBarHeight(mContext)); }else{ window.setLayout(metrics.widthPixels, this.getDialog().getWindow().getAttributes().height);//適配紅米6pro
 } window.setGravity(Gravity.BOTTOM);//設置在底部 //打開的動畫效果--從底部向上
 window.setWindowAnimations(R.style.bottomsheetdialog_animation); }
    }

    /**獲取狀態欄的高度*/
    private int getStatusBarHeight(Context context) {
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }
    @Override
    public void onDestroy()
    {
        //銷燬webview控件
        mWebView.removeAllViews();
        mWebView.destroy();
        super.onDestroy();
    }

    /**實例化控件*/
    private void initView() {
        mWebView = myView.findViewById(R.id.web_view);
        mWebView.setWebViewClient(new WebViewClient() {
            /**
             * 重寫此方法代表點擊網頁內的連接由本身處理,而不是新開Android的系統browser中響應該連接。
             */
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                //webView.loadUrl(url);
                return false;
            }
        });
    }
    /**
     * 初始化數據:tag標記、標題
     */
    private void initDatas() {
        mTag = this.getTag();

        mWebView.loadUrl("http://www.baidu.com");
    }

    /**
     * 初始化監聽事件
     */
    private void initEvents() {

    }

    @Override
    public void dismiss() {
        super.dismiss();
    }


}

動畫效果

3、使用方法

package com.why.project.normaldialogfragmentdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.why.project.normaldialogfragmentdemo.dialog.PreviewDialog;

public class MainActivity extends AppCompatActivity {

    private Button btn_open_full;
    private Button btn_open;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        initEvents();
    }

    private void initViews() {
        btn_open_full = findViewById(R.id.btn_open_full);
        btn_open = findViewById(R.id.btn_open);
    }

    private void initEvents() {
        btn_open_full.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打開預覽界面
                Bundle bundle = new Bundle(); PreviewDialog previewDialog = PreviewDialog.getInstance(MainActivity.this,bundle); previewDialog.show(getSupportFragmentManager(),PreviewDialog.TAG_FULLSCREEN);
            }
        });

        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打開預覽界面
                Bundle bundle = new Bundle(); PreviewDialog previewDialog = PreviewDialog.getInstance(MainActivity.this,bundle); previewDialog.show(getSupportFragmentManager(),PreviewDialog.TAG_BELOWSTATUEBAR);
            }
        });
    }
}

混淆配置

動畫

參考資料

暫時空缺this

項目demo下載地址

https://github.com/haiyuKing/NormalDialogFragmentDemo url

相關文章
相關標籤/搜索