设置AlertDialog的样式可在标题背景周围保留空白

时间:2018-09-06 18:12:31

标签: android android-alertdialog

我在我的应用程序中使用了三个对话框,一个对话框片段中一个DatePickerDialog,一个通过Alertdialog自定义布局的dialogfragment和一个没有dialogfragment的Alertdialog。我的dateopicker对话框看起来像这样。注意标题部分完全是橙色的。

datepickerdialog custom dialog

我的第二个对话框片段的标题部分没有覆盖我想要的整个窗口上部。

这是我的style.xml的相关部分

 <style name="ScheduleCompareTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryDarkColor</item>
    <item name="colorAccent">@color/primaryLightColor</item>
    <item name="android:dialogTheme">@style/ScheduleCompareDialogTheme</item>
    <item name="android:alertDialogTheme">@style/ScheduleCompareDialogTheme</item>
</style>

<style name="ScheduleCompareDialogTheme">
    <item name="android:windowTitleStyle">@style/ScheduleCompareDialogTitle</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:buttonBarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton</item>
</style>

<style name="ScheduleCompareDialogTitle">
    <item name="android:background">@color/primaryLightColor</item>
    <item name="android:textAppearance">@style/DialogWindowTitleText</item>
</style>

<style name="DialogWindowTitleText">
    <item name="android:textColor">@color/primaryTextColor</item>
    <item name="android:textSize">24sp</item>
</style>

第一部分是我的基本主题,第二部分是用于对话框和警报对话框的样式,第三部分是dialogtitle,第四部分是标题文本。样式有效,但是总标题区域的背景以某种方式不是橙色。从第二张图片来看,我假设应用了一些标准填充,因此我将style name="ScheduleCompareDialogTheme"中的填充设置为0dp。这产生了以下效果。

custom dialog with 0 padding

因此它有效,但仅适用于toppadding。将paddingLeft和paddingRight显式设置为0 dp不会产生任何结果。

在Internet上搜索时,我发现使用android:topDark有什么用,但这也没有效果。

有人知道如何扩展橙色矩形以覆盖整个顶部区域吗?

另外,我想提一下datepicker对话框已被设置为样式,就像使用基本主题显示的那样。

编辑: 这是定制对话框的布局文件。我删除了一些未在图片中显示的代码,但是当拥有更多权限的人使用该应用程序时会使用这些代码。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
xmlns:app="http://schemas.android.com/apk/res-auto">

    .......

        <RadioButton
            android:id="@+id/radio_teachers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Docenten" />



    .......

        <RadioButton
            android:id="@+id/radio_students"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Leerlingen" />


........


<AutoCompleteTextView
    android:id="@+id/acl_textinput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:hint="Zoeknaam"
    app:layout_constraintTop_toBottomOf="@id/check_own_properties"
    app:layout_constraintLeft_toLeftOf="@id/check_own_properties"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="50dp"
    android:dropDownHeight="150dp"
    android:inputType="textNoSuggestions"/>

</android.support.constraint.ConstraintLayout>

以及对话框片段的相关部分

public class OverlaySchedulePickerDialogFragment extends DialogFragment
    implements CompoundButton.OnCheckedChangeListener,
    View.OnClickListener {
private final String DEBUGTAG = "ScheduleDialog";

private Context mContext;
private ScheduleViewModel mScheduleViewModel;
private AlertDialog mDialog;
//variables for the layout
RadioGroup radiogroupBranches, radiogroupMainLeft, radiogroupMainRight;
RadioButton radioTeachers,radioStudents,radioLocations,radioGroups;
List<RadioButton> radioBranches;
CheckBox checkOwnProperties;
AutoCompleteTextView aclInputvalue;
StringsAdapter aclAdapter;

..... 

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    mScheduleViewModel = ViewModelProviders.of(getActivity()).get(ScheduleViewModel.class);
    mScheduleViewModel.loadDataFromDB(Constants.GETBRANCHESFROMDB, null);
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View fragmentLayout = inflater.inflate(R.layout.dialogfragment_secondschedulepicker, null);
    //get references to the layoutelements
    radiogroupMainLeft = fragmentLayout.findViewById(R.id.radiogroup_main_left);
    radiogroupMainRight = fragmentLayout.findViewById(R.id.radiogroup_main_right);
    radiogroupBranches = fragmentLayout.findViewById(R.id.radiogroup_branches);
    radioTeachers = fragmentLayout.findViewById(R.id.radio_teachers);
    radioStudents = fragmentLayout.findViewById(R.id.radio_students);

    aclInputvalue = fragmentLayout.findViewById(R.id.acl_textinput);

    //set listeners for the checkbox and radiobuttons
    checkOwnProperties.setOnCheckedChangeListener(this);
    radioStudents.setOnClickListener(this);
    radioTeachers.setOnClickListener(this);
    radioGroups.setOnClickListener(this);
    radioLocations.setOnClickListener(this);
    radioTeachers.performClick();

    //set properties of the AutoCompleteText
    aclAdapter = new StringsAdapter(mContext, R.layout.autocomplete_listitem,new ArrayList<>());
    aclInputvalue.setAdapter(aclAdapter);
    aclInputvalue.setThreshold(2);
    aclInputvalue.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            if(textView.getId() == aclInputvalue.getId() && actionId == IME_NULL && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                if (mDialog.getButton(DialogInterface.BUTTON_POSITIVE).isEnabled()) {
                    mDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
                    return true;
                } else {
                    if (aclAdapter.suggestions.size() > 0) {
                        String text = aclAdapter.suggestions.get(0);
                        aclInputvalue.setText(text);
                        aclInputvalue.setSelection(aclInputvalue.getText().length());
                        return true;
                    }
                }
            }
            return false;
        }
    });
    //add textWatcher to validate the entered text
    aclInputvalue.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String stringToValidate = editable.toString();
            if(mStringList!=null) {
                mDialog.getButton(DialogInterface.BUTTON_POSITIVE)
                        .setEnabled(mStringList.contains(stringToValidate));
            }
        }
    });

    //build the dialog
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
    dialogBuilder.setView(fragmentLayout);
    dialogBuilder.setTitle(R.string.overlayschedulepickerdialog_title);
    dialogBuilder.setPositiveButton(R.string.dialog_positive_button_text, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //Input is valid so transfer to mScheduleViewModel and ask for secondSchedule
            int position = mStringList.indexOf(aclInputvalue.getText().toString());
            mScheduleViewModel.setOverlaySchedule(mScheduleType,position);
        }
    });
    dialogBuilder.setNegativeButton(R.string.dialog_negative_button_text, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });
    mDialog = dialogBuilder.create();
    mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
        }
    });
    return mDialog;
}

END EDIT

0 个答案:

没有答案