【问题标题】:Android Fullscreen Fragment Dialog Background ClickableAndroid全屏片段对话框背景可点击
【发布时间】:2016-03-22 09:15:31
【问题描述】:

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

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

MainActivity.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;
}

}

dialog.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>

此按钮可点击,同时显示对话框。

提前致谢。

【问题讨论】:

  • 只需在onCreateView(...)中添加setCancelable(false);
  • @MD 我试过这个,但它是可点击的。

标签: java android android-dialogfragment dialogfragment


【解决方案1】:
<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" >

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

【讨论】:

    【解决方案2】:

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

    编辑

    或者,如果您希望通过按下后退按钮将其解除但仍防止触摸下方的元素,您可以致电 setCanceledOnTouchOutside(false)

    【讨论】:

    • 我曾尝试将 setCancelable(false) 设置为 onCreate() 或 onCreateView() 但不起作用。
    • @ssmm 你试过setCanceledOnTouchOutside(false)吗?
    • 我也试过了,还是不行。我认为,它不起作用,因为我没有使用 show() 方法调用对话框。以上答案对我有用,谢谢您的帮助
    • @ssmm 必须测试一下,无论如何很高兴您的问题得到解决
    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多