如何以编程方式设置 EditText 布局边距?
How to set EditText layout margin programmatically?
如何在 Android 中以编程方式设置 EditText 布局边距?我想为按下浮动操作按钮时出现的新 editText 设置边距。这是关于各种活动的输入,我想将它们放在一行中,每个活动都放在它之前的活动之下。请记住,这是约束布局。
我已经阅读了很多关于如何这样做的帖子,但似乎没有一个对我有用。
这是我的代码:
public class planlaeg extends AppCompatActivity {
FloatingActionButton fab;
Button button4;
private ConstraintLayout mLayout;
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_planlaeg);
button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openNewActivity();
}
});
TextView textView = findViewById(R.id.textView);
textView.setText(nyrejse.getValue());
mEditText = findViewById(R.id.editText);
fab = (FloatingActionButton) findViewById(R.id.fab);
mLayout = (ConstraintLayout) findViewById(R.id.planlaeg_l);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mLayout.addView(createNewEditText(mEditText.getText().toString()));
}
});
}
public void openNewActivity(){
Intent intent5 = new Intent(this, MainActivity.class);
startActivity(intent5);
}
private EditText createNewEditText(String text) {
final EditText et = new EditText(this);
et.setText(text);
return et;
}
}
检查 this answer for setting margin in general view. But if the EditText
is inside ConstraintLayout
you have to have constraint on the EditText for the side which you want to add margin. For example, if you want to add margin to the end of the view, you have to set layout_constraintEnd_toEndOf or layout_constraintEnd_toStartOf in the EditText. Check 以编程方式添加约束的答案。
如何在 Android 中以编程方式设置 EditText 布局边距?我想为按下浮动操作按钮时出现的新 editText 设置边距。这是关于各种活动的输入,我想将它们放在一行中,每个活动都放在它之前的活动之下。请记住,这是约束布局。
我已经阅读了很多关于如何这样做的帖子,但似乎没有一个对我有用。
这是我的代码:
public class planlaeg extends AppCompatActivity {
FloatingActionButton fab;
Button button4;
private ConstraintLayout mLayout;
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_planlaeg);
button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openNewActivity();
}
});
TextView textView = findViewById(R.id.textView);
textView.setText(nyrejse.getValue());
mEditText = findViewById(R.id.editText);
fab = (FloatingActionButton) findViewById(R.id.fab);
mLayout = (ConstraintLayout) findViewById(R.id.planlaeg_l);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mLayout.addView(createNewEditText(mEditText.getText().toString()));
}
});
}
public void openNewActivity(){
Intent intent5 = new Intent(this, MainActivity.class);
startActivity(intent5);
}
private EditText createNewEditText(String text) {
final EditText et = new EditText(this);
et.setText(text);
return et;
}
}
检查 this answer for setting margin in general view. But if the EditText
is inside ConstraintLayout
you have to have constraint on the EditText for the side which you want to add margin. For example, if you want to add margin to the end of the view, you have to set layout_constraintEnd_toEndOf or layout_constraintEnd_toStartOf in the EditText. Check