How to customize the width and height when show an Activity as a Dialog

前端 未结 5 1123
时光取名叫无心
时光取名叫无心 2020-12-08 19:41

If I have defined a Activity:

public class DialogActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        supe         


        
相关标签:
5条回答
  • 2020-12-08 20:21

    You can set the layout height and width in layout xml file

    You can even mention the values like "240px" for android:layout_width/android:layout_height

    0 讨论(0)
  • 2020-12-08 20:23

    I had similar problem, I used following theme for my activity

    android:theme="@style/Base.Theme.AppCompat.Light.Dialog"
    

    The following code fixed my problem

     setContentView(R.layout.activity_layout);
     getWindow().setLayout(width(pixels),height(pixels));
    
    0 讨论(0)
  • 2020-12-08 20:28

    I finally figured out one way to customize the size of my DialogActivity.

    That's inside the onCreate() method of DialogActivity, add the following code:

    WindowManager.LayoutParams params = getWindow().getAttributes();  
    params.x = -20;  
    params.height = 100;  
    params.width = 550;  
    params.y = -10;  
    
    this.getWindow().setAttributes(params); 
    
    0 讨论(0)
  • 2020-12-08 20:28

    There's a one-line code without hassling with the LayoutParams that is

    getWindow().setLayout((int)(width*.8),(int)(height*.25));
    

    This is what I've used before in one of my projects and now searching for, but found it in the same project.

    0 讨论(0)
  • 2020-12-08 20:29

    You can just change params of window in your acctivity like:

    override fun onStart() {
            super.onStart()
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        }
    
    0 讨论(0)
提交回复
热议问题