Android 底部对话框(选择拍照方式)

一、概述

项目中对于图片的上传需要一个底部的选择对话框,这里参考文章写了一个自己需要的对话框,效果图如下,参考文章

二、实现

Java文件:

  public void showSelectDialog() {
        bottomDialog = new Dialog(context, R.style.BottomDialog);//自定义样式
        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_bottom_picture, null);
        bottomDialog.setContentView(contentView);
        Button choosePhoto = (Button) contentView.findViewById(R.id.picture_gllary);
        Button takePhoto = (Button) contentView.findViewById(R.id.picture_camera);
        Button cancel = (Button) contentView.findViewById(R.id.picture_cancle);
        choosePhoto.setOnClickListener(this);
        takePhoto.setOnClickListener(this);
        cancel.setOnClickListener(this);

        //<editor-fold desc="距离下,左右边框距离,根据需求可删除或者增大">
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = getResources().getDisplayMetrics().widthPixels - dp2px(context, 16f);
        params.bottomMargin = dp2px(context, 8f);
        contentView.setLayoutParams(params);
        //</editor-fold>

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


     @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.picture_gllary:
                openGallery();
                bottomDialog.dismiss();
                break;
                ...
        }
         
    }

样式和动画设置:android:windowBackground属性一定要设置成透明,这样才能上下布局可以分离

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

XML布局文件:

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

    <LinearLayout
        android:id="@+id/dialog_btn_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/shape_dialog">

        <!--?android:attr/selectableItemBackground 点击有水波效果-->

        <Button
            android:id="@+id/picture_gllary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/dialog_btn_gallery_text"
            android:background="?android:attr/selectableItemBackground"
            android:textAllCaps="false"
            android:textSize="20dp"/>

        <View 
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="@android:color/darker_gray" />

        <Button
            android:id="@+id/picture_camera"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/dialog_btn_camera_text"
            android:background="?android:attr/selectableItemBackground"
            android:textAllCaps="false"
            android:textSize="20dp"/>

    </LinearLayout>

    <Button
        android:id="@+id/picture_cancle"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialog_btn_list"
        android:layout_centerInParent="true"
        android:text="@string/dialog_btn_cancel_text"
        android:background="@drawable/shape_btn_press_white"
        android:textAllCaps="false"
        android:textSize="20dp"/>
</RelativeLayout>

shape文件:shape_btn_press_white.xml 和shape_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item android:state_pressed="true"><shape>
            <solid android:color="#EBEBEB" />

            <corners android:radius="20dp"/>
        </shape></item>

        <item><shape>
            <solid android:color="@android:color/white" />
            <padding android:right="10dp" android:left="10dp"/>
            <corners android:radius="20dp"/>
        </shape></item>

</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <corners android:radius="20dp"/>
    <solid android:color="@android:color/white"/>
</shape>

Notes:如果这种上下按钮分离的效果,最外层的布局不要设置background

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值