其他分享
首页 > 其他分享> > 自定义警报对话框android

自定义警报对话框android

作者:互联网

我正在使用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> 

从这些样式中,我得到了带有边框的对话框.但我需要像下面这样.

我正在使用LG Nexus 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);
    }
}

结果:

标签:android-alertdialog,android,custom-controls,android-custom-view,customdialog
来源: https://codeday.me/bug/20191010/1885452.html