安卓全屏片段对话框背景可点击



我有全屏片段对话框。在显示时,背景对象可以单击或与用户交互。我该如何避免这种情况?

这是我如何显示对话框的代码段:

主要活动.java

FullScreenFrDialog fr = new FullScreenFrDialog();
FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
tr.add(android.R.id.content, fr, FR_TAG).commit();

FullScreenFrDialog.java

public class FullScreenFrDialog extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog, container, false);
    return view;
}

}

对话框.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFFFFFF" >
<ImageView
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
</FrameLayout>

main_activity.xml

<RelativeLayout 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" >
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
</RelativeLayout>

此按钮可在显示对话框时单击。

提前谢谢。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#CCFFFFFF" >

让父母点击真实,希望这对你有帮助。

在您的onCreate()中,只需调用setCancelable(false) 。这将防止在对话框外点击时关闭对话框,并且还将防止点击下方元素的事件

编辑

或者,如果您希望通过按下后退按钮将其关闭,但仍然可以阻止触摸下方的元素,则可以调用setCanceledOnTouchOutside(false)

最新更新