Android实现全屏的dialog
最近在做项目的时候,需要用到一个全屏的dialog。话不多说直接开整。
- 首先准备dialog的物料:
布局就比较简单,就两个TextView。
<?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="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/tv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/back" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/dialog_content" />
</LinearLayout>
接下来是dialog的样式和动画。
<style name="MyDialogStyle">
<item name="android:windowFullscreen">false</item>
<item name="android:windowNoTitle">true</item>
<!-- 进入和退出的动画 -->
<item name="android:windowAnimationStyle">@style/MyDialogAnimation</item>
</style>
<style name="MyDialogAnimation">
<!--进入 -->
<item name="android:windowEnterAnimation">@anim/dialog_enter</item>
<!--退出-->
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>
因为我想要dialog的进出动画跟activity的切换动画一样,所以就用了右进右出动画,一般的activity都是这样吧。
dialog_enter
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="200"
android:fromXDelta="100%"
android:toXDelta="0%" />
<alpha
android:duration="200"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
dialog_exit
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="200"
android:fromXDelta="0%"
android:toXDelta="100%" />
<alpha
android:duration="200"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
android:toXDelta=“100%”,表示自身的100%,也就是从View自己的位置开始。
android:toXDelta=“80%p”,表示父层View的80%,是以它父层View为参照的。
- 材料准备好后就来试试实例化dialog:
final Dialog dialog = new Dialog(context, R.style.MyDialogStyle);
View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_content, null);
contentView.findViewById(R.id.tv_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setContentView(contentView);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
dialog.show();
- 运行一下看看效果(API 26),居然就搞定了?虽然状态栏还在,但我觉得完全不影响,因为我就想要dialog看起来是一个activity的样子。
等等,那网上说的这种方式难道就不用了吗:
Window window = dialog.getWindow();
if (window != null) {
window.getDecorView().setPadding(0, 0, 0, 0);
window.getDecorView().setBackgroundColor(Color.WHITE);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
}
- 后来经过测试发现,在实例化dialog时如果给dialog设置了系统的一些样式,那么上面的window的一些方法是管用的。如果没有使用系统的一些样式,只是单独的设置一些属性比如 android:windowAnimationStyle,android:windowFullscreen那么上面window的方法是不起作用的。至于具体是哪个属性起了关键性的作用,后续我会继续测试排查。
- 所以如果你想要一个全屏的dialog可以用上面的办法,至于不想显示状态栏可以将android:windowFullscreen这个属性设置为true。为了更方便的处理一些业务逻辑我们可以自己简单封装一个dialog。
package com.getling.fullscreendialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class FullScreenDialog extends Dialog {
public FullScreenDialog(Context context) {
super(context, R.style.MyDialogStyle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_content, null);
contentView.findViewById(R.id.tv_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
setContentView(contentView);
}
}
-
如果想要完整项目,请移步GitHub自行下载
https://github.com/GetlingZ/FullScreenDialog
本人菜鸟一个,如果文中有什么不对的地方,欢迎各路大神指教。