How to set margin dynamically in Android?

后端 未结 5 2065
甜味超标
甜味超标 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:47

    Write below code to set margin, it may help you.

    AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
    Button button = new Button(Login.this);
    EditText editText = new EditText(Login.this);
    TextView textView = new TextView(Login.this);
    button.setText("Send");
    LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    buttonLayoutParams.setMargins(50, 10, 0, 0);
    button.setLayoutParams(buttonLayoutParams);
    String resetPassword = editText.getText().toString();
    LinearLayout layout = new LinearLayout(Login.this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.addView(textView);
    layout.addView(editText);
    layout.addView(button);
    myDialog.setView(layout);
    myDialog.show();
    

    Use LinearLayout.LayoutParams or RelativeLayout.LayoutParams according to parent layout of the child view

提交回复
热议问题