本篇博客主要介绍如何在使用Dialog的时候设置Dialog为铺满屏幕整个屏宽!!这里面主要有两点需要注意一下!!
Dialog的主题设置Dialog对应的Window的layoutParams参数
看一下截图吧:
截图显示的很清楚,dialog在宽度已经完全沾满整个屏幕宽度,而Dialog的显示的位置也不是系统的默认的居中显示,这一切都要归功于Window.LayoutParams,通过对参数的设置,我们可以控制Dialog对应的Window的宽度、高度,在屏幕上显示的位置等。相对与平常使用的UI上,可能不能再xml上那么方便的设置,不过也算是物有所值,有兴趣的可以自己试一下!!!
如果想要自己设置的LayoutParms参数生效,就必须为Dialog设置一个主题,如果不设置主题的话,Dialog会加载默认主题,会导致LayoutParam的部分参数不生效!!!
看一下不设置主题的Dialog截图吧!!
在默认的主题中,无法显示title,在java代码中,我也没有找到如何设置Dialog的Title!!!有知道的朋友可以告诉我一下!!!
两幅截图一对比就可以很明显的看出区别,LayoutParams的宽和高度设置没有生效,感觉设置的为内容包裹一样,当然可以通过设置DecorVew的宽高来改变Dialog的高宽,但是设置的宽度只是DecorView的宽度,实际显示的内容的宽度大小并没有发生改变!!!
相信这三幅截图一对比应该很能说明问题了!!! 现在看一下代码吧: 主题文件:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="Dialog_Fullscreen"> <item name="android:windowFullscreen">false</item> <item name="android:windowNoTitle">false</item> <item name="android:windowCloseOnTouchOutside">true</item> </style> </resources>Dialog布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:background="@color/colorAccent"> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_name" android:textAllCaps="false" android:layout_marginBottom="50dp"/> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_name" android:textAllCaps="false" /> </LinearLayout>背景添加了演示,方便演示区分!!!
主布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dialogdemo.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:onClick="showCustomDialog" android:text="@string/dialog" android:textAllCaps="false"/> </LinearLayout>java文件:
package com.example.dialogdemo; import android.animation.PropertyValuesHolder; import android.app.Dialog; import android.content.DialogInterface; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.transition.TransitionSet; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; public class MainActivity extends AppCompatActivity { private Dialog mDialog = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().getDecorView().setBackgroundColor(Color.GREEN); } private void getDialog(){ //设置全屏将会覆盖状态栏 mDialog = new Dialog(MainActivity.this,R.style.Dialog_Fullscreen); //mDialog = new Dialog(MainActivity.this); Window mWindow = mDialog.getWindow(); WindowManager.LayoutParams mParams = mWindow.getAttributes(); mParams.alpha = 1f; mParams.gravity = Gravity.CENTER_HORIZONTAL; mParams.width = WindowManager.LayoutParams.MATCH_PARENT; mParams.height = getResources().getDisplayMetrics().heightPixels/2; mParams.y = -200; mParams.x = 0; mWindow.setAttributes(mParams); mWindow.getDecorView().setBackgroundColor(Color.MAGENTA); mWindow.getDecorView().setPadding(0,0,0,0); mWindow.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels); mDialog.setContentView(R.layout.dialog_layout); mDialog.setCancelable(true); mDialog.setCanceledOnTouchOutside(true); mDialog.setTitle("this is title"); mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { Window mWindow = getWindow(); WindowManager.LayoutParams mParams = mWindow.getAttributes(); mParams.alpha = 1.0f; mWindow.setAttributes(mParams); } }); mDialog.show(); } @Override protected void onResume() { super.onResume(); // getDialog(); } public void showCustomDialog(View view){ getDialog(); } }这里有几点需要说明一下,首先的LayoutParams的x、y属性,代表的不是实际显示在屏幕中的位置,而是相对与原来位置的偏移的距离(弹框的原始位置肯定的居中显示嘛),所以这里的y设置为-200,意思是Dialog显示位置向上偏移200像素点!!
setCancelable方法表示是否允许点击back取消Dialog setCanceledOnTouchOutside方法表示是否允许点击弹框之外的部分取消Dialog
还有一点需要注意,如果你在显示Dialog的过程中设置了透明度,那么在弹框消失的时候需要手动将透明恢复为原来的值,透明度的值不会自动恢复的,那么你的activity的透明也是更改之后的值,有兴趣的朋友可以请测一下!!!(上面所有的运行结果均是在Android7.0的模拟器运行出来的)
好了,关于Dialog的问题就说到这了,谢谢大家的关注!!!
这是我的微信公众号,如果可以的话,希望您可以帮忙关注一下,这将是对我最大的鼓励了,谢谢!!
代码地址: https://github.com/zhuyuqiang2017/Other