Android全屏对话框

时间:2019-03-19 14:03:05

标签: android

我正在跟踪https://blog.alexanderschaefer.io/android-material-full-screen-fragment-dialog进行全屏对话框。

full_screen_dialog_layout

<?xml version="1.0" encoding="utf-8"?>
<android.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:paddingEnd="16dp"
            android:paddingStart="6dp"
            app:contentInsetStartWithNavigation="0dp"
            app:navigationIcon="@drawable/ic_close_white_24dp"
            android:paddingRight="16dp"
            android:paddingLeft="6dp" />
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Hello I am a dialog!" />
    </FrameLayout>
</android.coordinatorlayout.widget.CoordinatorLayout>

styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.FullScreenDialog" parent="Theme.MaterialComponents.Light.Dialog">
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="actionMenuTextColor">@color/colorAccent</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

FullScreenDialog.java

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.darekeapp.R;

public class FullScreenDialog extends DialogFragment {
    private Toolbar toolbar;

    public static FullScreenDialog display(FragmentManager fragmentManager) {
        FullScreenDialog fullScreenDialog = new FullScreenDialog();
        fullScreenDialog.show(fragmentManager, "example_dialog");
        return fullScreenDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme_FullScreenDialog);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setLayout(width, height);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.full_screen_dialog_layout,
                container);
        toolbar = view.findViewById(R.id.toolbar);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FullScreenDialog.this.dismiss();
            }
        });
        toolbar.setTitle("Some Title");
        toolbar.inflateMenu(R.menu.full_screen_dialog_menu);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                FullScreenDialog.this.dismiss();
                return true;
            }
        });
    }
}

Home.java

...
FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FullScreenDialog.display(getSupportFragmentManager());
            }
        });
...

popup_add_shift_log.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/company_name_text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">
            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/company_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/round_edt"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:hint="@string/company_name"
                android:inputType="text" />
        </android.support.design.widget.TextInputLayout>

        <android.support.v7.widget.SwitchCompat
            android:id="@+id/worked_for_agent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:checked="true"
            android:text="@string/worked_for_agent"
            android:textSize="18sp"
            android:textOn="@string/yes"
            android:textOff="@string/no"
            android:layout_below="@id/company_name_text_input_layout" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/agent_name_text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/worked_for_agent"
            android:layout_marginTop="10dp">
            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/agent_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/round_edt"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:hint="@string/agent_name"
                android:inputType="text" />
        </android.support.design.widget.TextInputLayout>

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/shift_start_question"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/agent_name_text_input_layout"
            android:text="@string/shift_start"
            android:textSize="18sp"
            android:textColor="@android:color/black"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp" />

        <com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker
            android:id="@+id/shift_start_time"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            app:picker_curved="true"
            app:picker_cyclic="true"
            android:layout_below="@id/shift_start_question"
            app:picker_visibleItemCount="7" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/shift_end_question"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/shift_start_time"
            android:text="@string/shift_end"
            android:textSize="18sp"
            android:textColor="@android:color/black"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp" />

        <com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker
            android:id="@+id/shift_end_time"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            app:picker_curved="true"
            app:picker_cyclic="true"
            android:layout_below="@id/shift_end_question"
            app:picker_visibleItemCount="7" />

        <android.support.v7.widget.SwitchCompat
            android:id="@+id/break_taken"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:checked="true"
            android:text="@string/break_taken"
            android:textSize="18sp"
            android:textOn="@string/yes"
            android:textOff="@string/no"
            android:layout_below="@id/shift_end_time" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/break_start_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/break_taken"
            android:text="@string/break_start_time"
            android:textSize="18sp"
            android:textColor="@android:color/black"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp" />

        <com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker
            android:id="@+id/break_start_time"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            app:picker_curved="true"
            app:picker_cyclic="true"
            android:layout_below="@id/break_start_text"
            app:picker_visibleItemCount="7" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/break_end_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/break_start_time"
            android:text="@string/break_end_time"
            android:textSize="18sp"
            android:textColor="@android:color/black"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp" />

        <com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker
            android:id="@+id/break_end_time"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            app:picker_curved="true"
            app:picker_cyclic="true"
            android:layout_below="@id/break_end_text"
            app:picker_visibleItemCount="7" />

        <android.support.v7.widget.SwitchCompat
            android:id="@+id/transport_job"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:checked="true"
            android:text="@string/transport_job"
            android:textSize="18sp"
            android:textOn="@string/yes"
            android:textOff="@string/no"
            android:layout_below="@id/break_end_time" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/transport_company_name_text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/transport_job"
            android:layout_marginTop="10dp">
            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/transport_company_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/round_edt"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:hint="@string/transport_company_name"
                android:inputType="text" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/vehicle_registration_text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/transport_company_name_text_input_layout"
            android:layout_marginTop="10dp">
            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/registration_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/round_edt"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:hint="@string/vehicle_registration"
                android:inputType="text" />
        </android.support.design.widget.TextInputLayout>
    </RelativeLayout>
</ScrollView>

我试图在单击FAB(浮动操作按钮)时显示全屏对话框。我想将视图设置为popup_add_shift_log。该应用不断崩溃,并显示各种错误,您可以看到我在代码中做错的任何事情

1 个答案:

答案 0 :(得分:3)

  

将对话框对象传递给此方法。

public void resizeDialogToFullscreen(Dialog dialog) {
        if (dialog != null && dialog.getWindow() != null) {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setLayout(width, height);
        }
    }