显示DialogFragment时在片段后面单击

时间:2019-06-01 07:16:56

标签: android dialogfragment

当显示DialogFragment时,我需要有权单击我后面的片段(例如单击按钮)。

但是当我在DialogFragment之外单击时,这将关闭。

使用代码显示 DialogFragment

FragmentManager fragmentManager = getFragmentManager();

MyDialogMapFragment dialogFragment = new MyDialogMapFragment();
dialogFragment.show( fragmentManager, "" );

MyDialogMapFragment 类:

public class MyDialogMapFragment extends DialogFragment {

    private Activity mActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.dialog_map_info, container, false);

        return rootView;

    }

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getActivity(), R.style.DialogMap);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_map_info);
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;

        dialog.getWindow().setAttributes(lp);

        return dialog;

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (context instanceof Activity) {
            mActivity = (Activity) context;
        }

    }

}

dialog_map_info.xml 布局:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="90dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

    <RelativeLayout
        android:id="@+id/mainFrame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:clickable="true"
        android:focusable="true"
        android:background="@drawable/background_map_info">

        <ProgressBar
            android:id="@+id/progress_loader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_centerInParent="true" 
            style="?android:attr/progressBarStyle" />

        <TextView
            android:id="@+id/itemTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Test" />

    </RelativeLayout>

</FrameLayout>

下图将描述我的布局:
Sample of my layout

1 个答案:

答案 0 :(得分:0)

如果需要在显示对话框时检测到单击,则将onClickListener设置为对话框上的视图会更容易。

调用onClickListener时,您可以单击按钮button.perfomClick(),甚至可以更好地调用按钮onClickListener调用的函数。