Dialog,ActivityDialog自定义位置,大小,动画等

activity

Dialog样式的Activity:

首先看自定义的样式
 
Manifest中配置



Activity代码
public class MessageAcitivty extends Activity {

    @BindView(R.id.mMsgTitle)
    TextView mTitle;
    @BindView(R.id.mMsgMessage)
    TextView mMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_acitivty);
        ButterKnife.bind(this);

        int screenWidth = ScreenUtils.getScreenWidth(this);

        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (screenWidth * 0.9);
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 这里还可以设置lp.x,lp.y在x轴,y轴上的坐标,只是这个位置是基于Gravity的
        window.setAttributes(lp);

        // 业务
        String msg = getIntent().getStringExtra("msg");
        if (!TextUtils.isEmpty(msg))
            mMessage.setText(msg);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 业务
        String msg = intent.getStringExtra("msg");
        if (!TextUtils.isEmpty(msg))
            mMessage.setText(msg);
    }
}
layout
这里注意根布局设置的属性,如果是wrap_content显示结果在水平方向上不是完全居中的
有大神知道原因的可以留言与我交流。

    
    ...


普通Dialog设置:

private Dialog dialog;

private void showSearchDialog() {
    if (dialog == null) {

        dialog = new Dialog(this, R.style.style_dialog);
        dialog.setContentView(popupView);
    //  dialog.setCancelable(false);
    //  dialog.setCanceledOnTouchOutside(false);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.TOP);
        window.setWindowAnimations(R.style.popWindowAnimation);
        window.getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = 900;
        lp.y = mToolbar.getHeight();
        window.setAttributes(lp);

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mView.setVisibility(View.GONE);
            }
        });
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                mView.setVisibility(View.VISIBLE);
            }
        });
    }
    dialog.show();
}
dialog动画Style

pop_show.xml


    
    
    
    

pop_hide.xml



    
    

    

你可能感兴趣的:(Dialog,ActivityDialog自定义位置,大小,动画等)