如何使我的对话框的宽度为fill_parent

时间:2016-04-02 05:53:56

标签: android android-layout android-dialog

如何使对话框适合android中屏幕的左,右,下角。

 googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                @Override
                public boolean onMarkerClick(Marker arg0) {

                    googleMap.getUiSettings().setMapToolbarEnabled(true);

                    final Dialog dialog = new Dialog(getActivity());
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.mapdialogbox);
                   // dialog.setCanceledOnTouchOutside(false);

                    Window window = dialog.getWindow();
                    WindowManager.LayoutParams wlp = window.getAttributes();
                    wlp.gravity = Gravity.BOTTOM ;
                    wlp.width = LinearLayout.LayoutParams.FILL_PARENT;
                    wlp.x = -100;
                    dialog.show();
     }
            });

This is how am getting by this code

但我希望我的对话框附在角落上,我该怎样才能实现呢?

5 个答案:

答案 0 :(得分:0)

Try this , may be its helpful for U.

wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;

答案 1 :(得分:0)

I did my code : you have to create custom style and set it to your dialog.
here is the my code :

 final Dialog dialog = new Dialog(context, R.style.CustomDialog);                                                              dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);                                                                 dialog.getWindow().setLayout(ViewGroup.LayoutParams.FILL_PARENT,enter code here ViewGroup.LayoutParams.FILL_PARENT);                                                                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                                                                View view = inflater.inflate(R.layout.full_screen_dialog, null);                                                                                                                                  cancel.setOnClickListener(new View.OnClickListener()                                                                     @Override                                                                   public void onClick(View v) {                                                                      dialog.dismiss();                                                                                                                                  });                                                              dialog.setContentView(view);                                                                dialog.setCancelable(false);                                                                dialog.show();

and here is the R.style.CustomDialog:

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/black</item>
        <item name="android:padding">80dp</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowIsFloating">false</item>
    </style>

Happy code: i hope it will help you.`enter code here`

答案 2 :(得分:0)

SetView() DialogBuilder方法上设置父视图。 然后使用设备屏幕的宽度为其添加一个子项。

View view = createPopView();

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true).setView(view);


private View createPopView() {

  LinearLayout layout = new LinearLayout(context);
  layout.setOrientation(LinearLayout.VERTICAL);

  EditText editText = new EditText(context);
  LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(screen_width,-2);

  layout.addView(editText);
  return layout;
}

答案 3 :(得分:0)

Remove any padding that you have on your layout.

答案 4 :(得分:0)

试试这个

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.your_layout);

getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}