Android dialog width

后端 未结 6 1816
天涯浪人
天涯浪人 2020-12-02 15:16

I can\'t seem to control the dialog width. I have a simple layout like so`




        
相关标签:
6条回答
  • 2020-12-02 15:51

    I'm using:

    getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    
    0 讨论(0)
  • 2020-12-02 15:55

    Try this. It works for me.

        dialog = new Dialog(Activity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.feedback_popup);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
    
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
    
        dialog.getWindow().setAttributes(lp);
    
    0 讨论(0)
  • 2020-12-02 15:58

    I had the same problem.

    I used following code to make dialog fill_parent and it worked fine.

    public class SharePost extends Dialog
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.adaptor_contentsharepost);
    
            LayoutParams params = getWindow().getAttributes();
            params.height = LayoutParams.FILL_PARENT;
            getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        }
    }
    

    layout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:id="@+id/dialogWidth"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        contents here
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-02 15:59

    Since API 8 FILL_PARENT is deprecated. Use MATCH_PARENT instead.

    params.height = LayoutParams.MATCH_PARENT;
    
    0 讨论(0)
  • 2020-12-02 16:01

    As Matthias points at out How can I get a Dialog style activity window to fill the screen? UMAR's solution works, but only if the window attributes are set AFTER setContentView() is called.

    0 讨论(0)
  • 2020-12-02 16:09

    Set a minimum width at the top most layout.

    android:minWidth="300dp"
    

    For example:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
      xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="300dp">
    
    <!-- Put remaining contents here -->
    
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题