码迷,mamicode.com
首页 > 移动开发 > 详细

Android编程入门--底部Dialog弹窗

时间:2017-09-29 14:57:02      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:public   nlog   utf-8   col   bsp   ontouch   gravity   list   tar   

参考博客:Android实现底部对话框BottomDialog

dialog_bottom

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:id="@+id/tv_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:drawableLeft="@drawable/ic_check_666666_24dp"
            android:drawablePadding="16dp"
            android:gravity="center_horizontal"
            android:padding="16dp"
            android:text="@string/dialog_ok"
            android:textColor="#666666"
            android:textSize="14sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:drawableLeft="@drawable/ic_close_666666_24dp"
            android:drawablePadding="16dp"
            android:gravity="center_horizontal"
            android:padding="16dp"
            android:text="@string/dialog_cancel"
            android:textColor="#666666"
            android:textSize="14sp" />
    </LinearLayout>
</LinearLayout>

 

style

<resources>
    <style name="BottomDialog" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

    <style name="BottomDialog.Animation" parent="Animation.AppCompat.Dialog">
        <item name="android:windowEnterAnimation">@anim/translate_dialog_in</item>
        <item name="android:windowExitAnimation">@anim/translate_dialog_out</item>
    </style>
</resources>

anim

translate_dialog_in

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="300"
           android:fromXDelta="0"
           android:fromYDelta="100%"
           android:toXDelta="0"
           android:toYDelta="0">
</translate>

translate_dialog_out

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="300"
           android:fromXDelta="0"
           android:fromYDelta="0"
           android:toXDelta="0"
           android:toYDelta="100%">
</translate>

代码如下

    @OnClick(R.id.log_out_layout)
    public void onLogOutClicked() {
        final Dialog mDialog = new Dialog(this.getActivity(), R.style.BottomDialog);
        View contentView = LayoutInflater.from(this.getActivity()).inflate(R.layout.dialog_bottom, null);
        mDialog.setContentView(contentView);

        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = getResources().getDisplayMetrics().widthPixels;
        contentView.setLayoutParams(layoutParams);

        mDialog.getWindow().setGravity(Gravity.BOTTOM);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        mDialog.show();

        TextView tvOk = (TextView) contentView.findViewById(R.id.tv_ok);
        tvOk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                System.exit(0);//系统退出
            }
        });

        TextView tvCancel = (TextView) contentView.findViewById(R.id.tv_cancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mDialog.cancel();
            }
        });
    }

 

Android编程入门--底部Dialog弹窗

标签:public   nlog   utf-8   col   bsp   ontouch   gravity   list   tar   

原文地址:http://www.cnblogs.com/bmbh/p/7198771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!