一、概述
项目中对于图片的上传需要一个底部的选择对话框,这里参考文章写了一个自己需要的对话框,效果图如下,参考文章
二、实现
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