问题描述:

I have tried to create a dialog that occupies the full screen width from the old AlertDialog builder to the new DialogFragment approach in the onCreateView() and onViewCreated() to get the displayed dialog to occupy the full width of the screen. I can certainly get the width and height values of the screen but regardless of how I try to force the dialog to use these values, they are ignored. The displayed dialog is always the same width regardless of orientation.

In my latest attempt I have an xml layout that I inflate. I need to use a custom view so I cannot define that view in xml. So I add it.

Here is the most current attempt I have in my DialogFragment code. Of course this is just one of many attempts I have made trying to follow hints from posts and Slidenerd videos.

public class PopupDialog extends DialogFragment implements View.OnClickListener

{

private static final String TAG = PopupDialog.class.getName();

Button cancel = null;

Button focus = null;

View viewInput = null;

int width;

int height;

int id;

public PopupDialog()

{

}

public PopupDialog(View v, int id, int width, int height)

{

viewInput = v;

this.id = id;

this.width = width;

this.height = height;

}

@Override

public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstance)

{

Log.d(TAG, "onCreateView of DialogFragment called.");

View viewDialog = inflator.inflate(R.layout.popup_dialog, null);

// RelativeLayout relativeLayout = (RelativeLayout)viewDialog;

// LayoutParams params = new LayoutParams(width, height);

// relativeLayout.setLayoutParams(params);

// Point point = new Point();

// Activity activity = getActivity();

// activity.getWindowManager().getDefaultDisplay().getSize(point);

// if(point.x > point.y)

if(width > height)

{

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}

else

{

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

ViewParent parent = viewInput.getParent();

if(parent != null)

{

Log.d(TAG, "View already present. Removing.");

((ViewGroup)parent).removeView(viewInput);

}

LayoutParams params = new LayoutParams(width, height);

viewInput.setLayoutParams(params);

((ViewGroup)viewDialog).addView(viewInput, 0);

cancel = (Button)viewDialog.findViewById(R.id.btn_cancel);

focus = (Button)viewDialog.findViewById(R.id.btn_focus);

cancel.setOnClickListener(this);

focus.setOnClickListener(this);

setCancelable(false);

return viewDialog;

}

@Override

public void onViewCreated(View view, Bundle savedInstanceState)

{

super.onViewCreated(view, savedInstanceState);

Log.d(TAG, "onViewCreated of DialogFragment called.");

//getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, height);

getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();

wmlp.gravity = Gravity.TOP | Gravity.LEFT;

wmlp.x = 10; //x position

wmlp.y = 450 * (id) + 10;

// wmlp.width = width;

// wmlp.height = height;

}

I am plotting a sine wave. The view has the correct size as the sine wave has a range of 0 to 12 but in the landscape orientation the displayed dialog box only gets a little more than half way, so 0 to 6 + is seen and then one has to wait for the wave to recycle as it plots from 6 to 12 before it becomes visible again when it goes back to 0. I AM able to place the dialog box upper left hand corner.

Does anyone know how to solve this problem? I went to the fragment because I was led to believe that the canned AlertDialog approach was fixed in width and there was nothing one could do. I am facing the same limitation with the DialogFragment.

网友答案:

try adding this code in on create() method after setContentView

getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
相关阅读:
Top