自定义警告对话框的android [英] Custom alert dialog android

查看:181
本文介绍了自定义警告对话框的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的定制警报对话框的工作。

I am working with custom alert dialog in android.

我按照链接1 和<一个href=\"http://stackoverflow.com/questions/9305581/how-to-make-a-dialog-slide-from-bottom-to-middle-of-screen-in-android\">linke 2 。

在使用这些风格我的code。

In my code using these styles.

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

从这些样式我得到接壤对话框。但是,我需要像下面。

From these styles I am getting bordered dialog. But I need like the below.

我使用LG的Nexus 4设备。我应该要做的实现这一目标?

I am using LG Nexus 4 device. What should I have to do to achieve this?

推荐答案

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

I have some solution. Please look at the example below.

在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文件:

And dialog java file:

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);
    }
}

结果:

这篇关于自定义警告对话框的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆