自定义警报对话框android

时间:2015-02-12 18:03:56

标签: android custom-controls android-custom-view android-alertdialog customdialog

我在android中使用自定义警报对话框。

我已关注link 1linke 2

在我使用这些样式的代码中。

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/slide_up_dialog</item>
    <item name="android:windowExitAnimation">@anim/slide_out_down</item>

<!-- Animation for dialog box -->
<style name="DialogSlideAnim" parent="AppBaseTheme">
    <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
</style> 

从这些风格我得到了边界对话。但我需要如下所示。Custom alert dialog

我正在使用LG Nexus 4设备。我应该怎么做才能实现这个目标?

1 个答案:

答案 0 :(得分:4)

我有一些解决方案。请看下面的例子。

在样式xml中使用:

<resources>
    ....
    <style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/abc_slide_in_bottom</item>
        <item name="android:windowExitAnimation">@anim/abc_slide_out_bottom</item>
    </style>

    <style name="DialogSlideAnim">
        <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>

和对话框布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Male"
            android:id="@+id/male"
            android:layout_marginBottom="-10dp"
            android:layout_gravity="center_horizontal"/>
    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Female"
            android:id="@+id/female"
            android:layout_below="@id/male"
            android:layout_gravity="center_horizontal"/>
    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Cancel"
            android:layout_below="@id/female"
            android:id="@+id/cancel"
            android:layout_gravity="center_horizontal"/>
</RelativeLayout>

对话框java文件:

public class YourDialog extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new Dialog(getActivity(), R.style.DialogSlideAnim);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.your_dialog, container, false);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        final Window window = getDialog().getWindow();
        window.setGravity(Gravity.BOTTOM);
        lp.copyFrom(window.getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }
}

结果:

enter image description here