BotomDialog

链接传送门

  1. 获取dailog实例

         private void showDialog() {
         Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);
     //        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_content_circle, null);
             View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_content_nomal, null);
             dialog.setContentView(contentView);
             ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
     //        params.width = getResources().getDisplayMetrics().widthPixels - DensityUtil.dp2px(getActivity(), 16f);
     //        params.bottomMargin = DensityUtil.dp2px(getActivity(), 8f);
             params.width = getResources().getDisplayMetrics().widthPixels;
         contentView.setLayoutParams(params);
         dialog.setCanceledOnTouchOutside(true);
         dialog.getWindow().setGravity(Gravity.BOTTOM);
         dialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
         dialog.show();
     }
    
  2. 在style中配置相关的动画和dialog主题

    <style name="BottomDialog" parent="@style/Base.V7.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>
  1. anim中dialog的动画

    • translate_dialog_in.xml

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

        <?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>
      
  2. 像素转换工具类

    import android.content.Context;
    import android.util.TypedValue;

    public class DensityUtil {
        /**
         * dp转px
         *
         * @param context
         * @param dpVal
         * @return
         */
        public static int dp2px(Context context, float dpVal) {
            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal,
                    context.getResources().getDisplayMetrics());
        }
    
        /**
         * sp转px
         *
         * @param context
         * @param spVal
         * @return
         */
        public static int sp2px(Context context, float spVal) {
            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal,
                    context.getResources().getDisplayMetrics());
        }
    
        /**
         * px转dp
         *
         * @param context
         * @param pxVal
         * @return
         */
        public static float px2dp(Context context, float pxVal) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (pxVal / scale);
        }
    
        /**
         * px转sp
         *
         * @param pxVal
         * @param pxVal
         * @return
         */
        public static float px2sp(Context context, float pxVal) {
            return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
        }
    }
  1. layout相关布局

         <?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="@drawable/shape_dialog"
                   android:orientation="vertical">
     
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="?android:attr/selectableItemBackground"
             android:clickable="true"
             android:drawableLeft="@drawable/ic_edit"
             android:drawablePadding="16dp"
             android:gravity="center_vertical"
             android:padding="16dp"
             android:text="设置备注及标签"
             android:textColor="#666666"
             android:textSize="14sp"/>
     
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="?android:attr/selectableItemBackground"
             android:clickable="true"
             android:drawableLeft="@drawable/ic_star"
             android:drawablePadding="16dp"
             android:gravity="center_vertical"
             android:padding="16dp"
             android:text="标为星标朋友"
             android:textColor="#666666"
             android:textSize="14sp"/>
     
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="?android:attr/selectableItemBackground"
             android:clickable="true"
             android:drawableLeft="@drawable/ic_circle"
             android:drawablePadding="16dp"
             android:gravity="center_vertical"
             android:padding="16dp"
             android:text="设置朋友圈权限"
             android:textColor="#666666"
             android:textSize="14sp"/>
     
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="?android:attr/selectableItemBackground"
             android:clickable="true"
             android:drawableLeft="@drawable/ic_send"
             android:drawablePadding="16dp"
             android:gravity="center_vertical"
             android:padding="16dp"
             android:text="发送该名片"
             android:textColor="#666666"
             android:textSize="14sp"/>
     
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="?android:attr/selectableItemBackground"
             android:clickable="true"
             android:drawableLeft="@drawable/ic_alert"
             android:drawablePadding="16dp"
             android:gravity="center_vertical"
             android:padding="16dp"
             android:text="投诉"
             android:textColor="#666666"
             android:textSize="14sp"/>
     
         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="?android:attr/selectableItemBackground"
             android:clickable="true"
             android:drawableLeft="@drawable/ic_delete"
             android:drawablePadding="16dp"
             android:gravity="center_vertical"
             android:padding="16dp"
             android:text="删除"
             android:textColor="#666666"
             android:textSize="14sp"/>
     </LinearLayout>

推荐阅读更多精彩内容