对话框 dialog 整理

1、简单的对话框(默认布局):

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("尊敬的用户");
builder.setMessage("你真的要卸载我吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        
    }
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        
    }
});
AlertDialog alert = builder.create();
alert.show();

2、带选项的对话框:

AlertDialog.Builder builder=new AlertDialog.Builder(InfoActivity.this);
builder.setTitle("头像");
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});
String[] items=new String[]{"相册上传","拍摄上传","下载头像"};
builder.setItems(items, new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if(which == 0){
        //从相册获取图片
//            Intent intent = new Intent(Intent.ACTION_PICK, null);
//            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
//            startActivityForResult(intent, PHOTO_ZOOM);
        }
        else if(which == 1){
        //从拍照获取图片
//            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment
//                    .getExternalStorageDirectory(),"temp.jpg")));
//            startActivityForResult(intent, PHOTO_GRAPH);
        }
        else if(which == 2){
//            Matrix m = image.getImageMatrix();
        }
    }
});
builder.create().show();

3、自定义对话框 修改宽度、高度

编写一个对话框样式,这里可以修改颜色,有了这个样式,对话框才可以自定义宽度和高度(如果不考虑宽高的话,这步可以省略):

<style name="DialogShareTheme" parent="Theme.AppCompat.Dialog">
    <item name="android:background">@color/white</item>
    <item name="android:windowBackground">@color/transparent</item>
</style>

接着,编写自定义对话框的布局文件:dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

最后,就可以在java中设置其宽度高度了:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ProductionInformationActivity.this, R.style.DialogShareTheme); //加入样式
final View dialogView = LayoutInflater.from(ProductionInformationActivity.this).inflate(R.layout.dialog,null);  //加入布局
dialogBuilder.setView(dialogView);
Dialog dialog = dialogBuilder.create();
dialog.getWindow().setGravity(Gravity.BOTTOM);
dialog.show();
WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
p.width = MyApp.getScreenWidth(this); //设置dialog的宽度为当前手机屏幕的宽度
dialog.getWindow().setAttributes(p);

4、将对话框修改成圆角的(或修改颜色):

定义一个shape文件,然后就可以在java中使用了:

Window window = dialog_demo.getWindow();
window.setBackgroundDrawable(getDrawable(R.drawable.shape_dialog_demo));

也可以在style中定义对话框的背景为纯透明,然后在其布局文件中修改圆角;

5、给对话框添加动画:

先添加一个动画文件(res->anim文件夹中),文件名为a.xml

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

    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="3000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0">
    </scale>

</set>

接着在style.xml文件中加上:

//对话框动画
<style name="dialog_animation" parent="@android:style/Animation.Dialog">
    <item name="android:windowEnterAnimation">@anim/a</item>
    <!--<item name="android:windowExitAnimation">@anim/a</item>-->
</style>

最后在java中调用:

Window window = dialog.getWindow();
window.setWindowAnimations(R.style.dialog_animation);

 

6、弹出时Activity背景不变暗

01:在style 的xml 里 设置如下
<resources>
    <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:backgroundDimEnabled">false</item><!--activity不变暗-->
    </style>
</resources>
设置为true, 背景变暗

Dialog = new Dialog(Activity, R.style.dialog);

02:在代码中 可以这么设置
Window mWindow = getWindow();  
WindowManager.LayoutParams lp = mWindow.getAttributes();  
lp.dimAmount =0f;
背景就不会变暗了

 

7、控制对话框的出现位置

WindowManager.LayoutParams params = window.getAttributes(); 获取window 的params 然后给params去设置x y 参数即可 我们设置的 x y 是相对值 相对自身位置的偏移量 负值无效(显而易见 x 是代表水平方向的偏移 y 代表竖直方向的偏移) 

简单举个栗子

  * 当参数值包含Gravity.LEFT时,对话框出现在左边,所以params.x就表示相对左边的偏移

    * 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以params.x就表示相对右边的偏移

    * 当参数值包含Gravity.TOP时,对话框出现在上边,所以params.y就表示相对上边的偏移

    * 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以params.y就表示相对下边的偏移

注意 负值无效

还是刚才那个需求 我有一个dialog 想让它显示在距离顶部100 的位置

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

View view = LayoutInflater.from(getActivity()).inflate(R.layout.home_identity_choose_window, null); 

builder.setView(view); 

titleDialog = builder.create(); 

Window window = titleDialog.getWindow(); 

WindowManager.LayoutParams params = window.getAttributes(); 

params.x = 10; 

params.y = 100; 

params.width = 220; 

params.height = 200; 

window.setAttributes(params); 

titleDialog.show(); 

window.setGravity(Gravity.TOP);

 

 


posted on 2019-05-09 15:45  赵子隆  阅读(205)  评论(0编辑  收藏  举报

导航