How to set margin dynamically in Android?

后端 未结 5 2068
甜味超标
甜味超标 2020-12-25 11:19

I am currently doing an android application that contains customize alert dialog. It contains a button , but i can\'t set the margin for the button . the code is given belo

5条回答
  •  不知归路
    2020-12-25 11:48

    You can set in LinearLayout margin
    
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams layoutParams = new
    LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(30, 20, 30, 0);
    Button okButton=new Button(this);
    okButton.setText("some text");
    ll.addView(okButton, layoutParams);
    

提交回复
热议问题