문제

I'm trying to get the width of a DialogFragment on runtime, I've tried two methods which failed.

First: inside onCreateDialog after the dialog has been created

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    ...
    ...
    AlertDialog dialog =  builder.create();
    width = dialog.getWindow().getAttributes().width;
    return dialog;
}   

width was set to -1

Second:

public void onStart() {
    width = getDialog().getWindow().getAttributes().width;
}

width was set to -2

도움이 되었습니까?

해결책

Solved it using a ViewTreeObserver set in onCreateDialog

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top