当前位置:首页> 代码示例 > Java >正文

```java

import android.widget.FrameLayout; //导入方法依赖的package包/类

/**

* 返回对话框的根视图。我们将其包装在FrameLayout中,因为这是系统通知我们对话框大小已更改的方式。

* 这有一个令人愉快的副作用,即我们不需要在所有模态框前加上 "top: statusBarHeight",

* 因为那个边距将包含在FrameLayout中。

*/

private View getContentView() {

FrameLayout frameLayout = new FrameLayout(getContext());

frameLayout.addView(mHostView);

frameLayout.setFitsSystemWindows(true);

return frameLayout;

}

```

```java

import android.widget.FrameLayout;

import android.widget.Activity;

import android.view.WindowManager;

import android.view.View;

import android.graphics.PixelFormat;

public void setParentActivity(Activity activity) {

if (parentActivity == activity) {

return;

}

parentActivity = activity;

windowView = new FrameLayout(activity);

windowView.setFocusable(true);

windowView.setFocusableInTouchMode(true);

if (Build.VERSION.SDK_INT >= 23) {

windowView.setFitsSystemWindows(true);

}

containerView = new FrameLayoutDrawer(activity);

containerView.setFocusable(false);

windowView.addView(containerView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT));

containerView.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_POINTER_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {

close();

}

return true;

}

});

windowLayoutParams = new WindowManager.LayoutParams();

windowLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;

windowLayoutParams.format = PixelFormat.TRANSLUCENT;

windowLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;

windowLayoutParams.gravity = Gravity.TOP;

windowLayoutParams.type = WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;

if (Build.VERSION.SDK_INT >= 21) {

windowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;

} else {

windowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

}

centerImage.setAspectFit(true);

centerImage.setInvalidateAll(true);

centerImage.setParentView(containerView); }

```

以下是重构后的代码:

```java

import android.widget.FrameLayout;

import android.app.Activity;

import android.view.View;

import android.graphics.Color;

import android.view.ViewGroup;

import android.view.Gravity;

import androidx.annotation.NonNull;

import androidx.core.content.ContextCompat;

import com.yourpackagename.R; // 替换为实际的包名和资源文件名

public class YourClassName { // 替换为实际的类名

private Activity parentActivity;

private FrameLayout windowView;

private FrameLayoutDrawer containerView;

private View centerImage;

private SecretDeleteTimer secretDeleteTimer;

private WindowManager.LayoutParams windowLayoutParams;

public void setParentActivity(@NonNull Activity activity) {

if (parentActivity == activity) {

return;

}

parentActivity = activity;

windowView = new FrameLayout(activity);

windowView.setBackgroundColor(0xff000000);

windowView.setFocusable(true);

windowView.setFocusableInTouchMode(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Android M引入了fitsSystemWindows属性,需要检查API级别是否大于等于23

windowView.setFitsSystemWindows(true);

}

containerView = new FrameLayoutDrawer(activity);

containerView.setFocusable(false);

windowView.addView(containerView);

FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)containerView.getLayoutParams();

layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;

layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;

layoutParams.gravity = Gravity.TOP | Gravity.LEFT;

containerView.setLayoutParams(layoutParams);

containerView.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_POINTER_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {

closePhoto();

}

return true;

}

});

secretDeleteTimer = new SecretDeleteTimer(activity);

containerView.addView(secretDeleteTimer);

layoutParams = (FrameLayout.LayoutParams)secretDeleteTimer.getLayoutParams();

layoutParams.gravity = Gravity.TOP | Gravity.RIGHT;

layoutParams.width = ContextCompat.getDrawableDimension(activity, R.drawable.ic_delete_black_24dp); // 需要替换为实际的图标资源ID或资源文件名

layoutParams.height = ContextCompat.getSizeDimension(activity, R.dimen.toolbar_top_padding); // 需要替换为实际的尺寸资源ID或资源文件名

layoutParams.rightMargin = ContextCompat.getSizeDimension(activity, R.dimen.toolbar_top_padding); // 需要替换为实际的尺寸资源ID或资源文件名

layoutParams.topMargin = ContextCompat.getSizeDimension(activity, R.dimen.toolbar_top_padding); // 需要替换为实际的尺寸资源ID或资源文件名

secretDeleteTimer.setLayoutParams(layoutParams);

windowLayoutParams = new WindowManager.LayoutParams();

windowLayoutParams.height = WindowManager

```java

import android.widget.FrameLayout; // 导入方法依赖的package包/类

public void setParentActivity(Activity activity) {

if (parentActivity == activity) {

return;

}

parentActivity = activity;

windowView = new FrameLayout(activity);

windowView.setBackgroundColor(0xff000000);

windowView.setFocusable(true);

windowView.setFocusableInTouchMode(true);

if (Build.VERSION.SDK_INT >= 23) {

windowView.setFitsSystemWindows(true); //TODO ?

}

containerView = new FrameLayoutDrawer(activity);

containerView.setFocusable(false);

windowView.addView(containerView);

FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)containerView.getLayoutParams();

layoutParams.width = LayoutHelper.MATCH_PARENT;

layoutParams.height = LayoutHelper.MATCH_PARENT;

layoutParams.gravity = Gravity.TOP | Gravity.LEFT;

containerView.setLayoutParams(layoutParams);

containerView.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_POINTER_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {

closePhoto();

}

return true;

}

});

secretDeleteTimer = new SecretDeleteTimer(activity);

containerView.addView(secretDeleteTimer);

layoutParams = (FrameLayout.LayoutParams)secretDeleteTimer.getLayoutParams();

layoutParams.gravity = Gravity.TOP | Gravity.RIGHT;

layoutParams.width = AndroidUtilities.dp(100);

layoutParams.height = AndroidUtilities.dp(32);

layoutParams.rightMargin = AndroidUtilities.dp(19);

layoutParams.topMargin = AndroidUtilities.dp(19);

secretDeleteTimer.setLayoutParams(layoutParams);

windowLayoutParams = new WindowManager.LayoutParams();

windowLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;

windowLayoutParams.format = PixelFormat.TRANSLUCENT;

windowLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;

windowLayoutParams.gravity = Gravity.TOP;

windowLayoutParams.type = WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;

windowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

centerImage.setParentView(containerView);

}

```