本文以一个选择图库和照片为例:
需要三个元素:1弹窗布局,2style主题,设置Dialog类
先看弹窗布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/ll_icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/my_dialog_item"
android:orientation="vertical">
<TextView
android:id="@+id/btn_take_pic"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:clickable="true"
android:gravity="center"
android:text="拍照"
android:textColor="@color/color_black_4e4e4e"
android:textSize="@dimen/s_16sp" />
<View
android:id="@+id/view_01"
android:layout_width="match_parent"
android:layout_height="@dimen/s_0d5dp"
android:background="@drawable/line_night_color" />
<TextView
android:id="@+id/btn_get_album_pic"
android:layout_width="match_parent"
android:layout_height="@dimen/s_50dp"
android:layout_centerVertical="true"
android:clickable="true"
android:gravity="center"
android:text="从相册选择"
android:textColor="@color/color_black_4e4e4e"
android:textSize="@dimen/s_16sp" />
</LinearLayout>
<TextView
android:id="@+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="@dimen/s_50dp"
android:layout_centerVertical="true"
android:layout_marginTop="@dimen/s_10dp"
android:background="@drawable/my_dialog_item"
android:clickable="true"
android:gravity="center"
android:text="取消"
android:textColor="@color/color_black_4e4e4e"
android:textSize="@dimen/s_16sp"
/>
</LinearLayout>
再看在values中建一个styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- 自定义底部弹出dialog样式-->
<style name="PictureDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowEnterAnimation">@anim/mylogout_dialog_enter</item>
<item name="android:windowExitAnimation">@anim/mylogout_dialog_exit</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/my_dialog</item>
<item name="android:windowIsFloating">true</item>
<!--<item name="android:windowContentOverlay">@null</item>-->
</style>
<style name="my_item_layout">
<item name="android:gravity">center_vertical</item>
<!--<item name="android:background">@drawable/ripple_bg</item>-->
<item name="android:paddingLeft">@dimen/s_16dp</item>
<item name="android:paddingRight">@dimen/s_16dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">@dimen/s_47dp</item>
<item name="android:clickable">true</item>
</style>
<style name="my_item_text">
<item name="android:textSize">@dimen/s_16sp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">@color/color_black_333333</item>
</style>
<style name="my_item_pic">
<item name="android:layout_alignParentRight">true</item>
<item name="android:layout_marginRight">@dimen/s_5dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
<item name="android:src">@drawable/jiantou</item>
<item name="android:scaleType">fitCenter</item>
</style>
</resources>
Dialog类:
public class SetPhoneDialog {
private Context mContext;
Dialog dialog;
public TextView btnTakePic,btnGetAlbumPic,btnCancel;
public SetPhoneDialog(Context mContext) {
this.mContext = mContext;
}
public void dialogSet(final ArticleMenuOnClickListener listener){
dialog = new Dialog(mContext, R.style.PictureDialog);
dialog.setContentView(R.layout.menu_dialog_headimg);
btnTakePic = (TextView) dialog.findViewById(R.id.btn_take_pic);
btnGetAlbumPic = (TextView)dialog.findViewById(R.id.btn_get_album_pic);
btnCancel = (TextView)dialog.findViewById(R.id.btn_cancel);
//获取到dialog窗口
Window dialogWindow = dialog.getWindow();
//设置窗口在activity的最底下
dialogWindow.setGravity(Gravity.BOTTOM);
dialogWindow.setWindowAnimations(R.style.PictureDialog); //设置窗口弹出动画
WindowManager m = ((Activity) mContext).getWindowManager();
Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
p.width = (int) (d.getWidth() * 1); // 宽度设置为屏幕的百分之百
dialogWindow.setAttributes(p);
dialog.show();
//设置取消按钮点击事件
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View paramView) {
dialog.dismiss();
}
});
//设置拍照按钮点击事件
btnTakePic.setOnClickListener(new View.OnClickListener() {
public void onClick(View paramView) {
dialog.dismiss();
//take a picture
listener.btn_PaiZhaoListener();
}
});
//设置从相册选择按钮点击事件
btnGetAlbumPic.setOnClickListener(new View.OnClickListener() {
public void onClick(View paramView) {
dialog.dismiss();
listener.btn_XiangJiListener();
}
});
}
//点击事件接口
public interface ArticleMenuOnClickListener{
public void btn_PaiZhaoListener();
public void btn_XiangJiListener();
}
}
在activity中实现:
//头像点击事件
case R.id.head_image:
SetPhoneDialog diaolog = new SetPhoneDialog(getActivity());
diaolog.dialogSet(new SetPhoneDialog.ArticleMenuOnClickListener() {
@Override
public void btn_PaiZhaoListener() {
//方法回调,拍照
try {
Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//下面这句指定调用相机拍照后的照片存储的路径
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME)));
startActivityForResult(takeIntent, REQUEST_CODE_TAKE);
}catch (Exception e){
Toast.makeText(getActivity(),"请打开手机相机权限!",Toast.LENGTH_SHORT).show();
}
}
效果图: