How to get size of a custom dialog in android?

2019-04-06 23:44发布

I have a custom Dialog which is extended from dialog. in order to place component on it, I need to know the width of the dialog. how could I do it? Thanks

EDIT:

public class AnswerTypeDialog extends Dialog{
    public AnswerTypeDialog(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    protected void init(){
        ll=(RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.answertype_layout, null);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(ll);
        mWindowWidth = this.getWindow().getAttributes().width;
    }
}

mWindowWidth is zero.

2条回答
Bombasti
2楼-- · 2019-04-06 23:49

You can use this snippet

int height=mDialog.getWindow().getDecorView().getHeight();
int width=mDialog.getWindow().getDecorView().getWidth();
查看更多
劳资没心,怎么记你
3楼-- · 2019-04-07 00:06

This will work.

@Override
    public void onWindowFocusChanged (boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        int height=mDialog.getWindow().getDecorView().getHeight();
    int width=mDialog.getWindow().getDecorView().getWidth();
}
查看更多
登录 后发表回答