0

目标

我有一个在 xml 中有一些 TextView 集的 RelativeLayout,我想动态添加一些 EditText,一个在另一个之下。

问题

我怎样才能水平居中那些EditText的?

代码

这是我的最新代码:

/* Layout with EditText's */
RelativeLayout parentLayout = (RelativeLayout)
findViewById(R.id.relativeLayoutLectura);
EditText editText = new EditText(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// If it's first EditText to insert, goes below another linearLayout set before
if(id == -1) {
    editText.setId(1);
    params.addRule(RelativeLayout.BELOW, R.id.linearLayoutMensajeLectura);
} else {
    editText.setId(id);
    params.addRule(RelativeLayout.BELOW, id-1);
}
editText.setLayoutParams(params);
editText.setGravity(Gravity.CENTER_HORIZONTAL); //It does nothing
parentLayout.addView(editText);

提前致谢。

4

1 回答 1

0

设置编辑文本的重力将影响文本在其中显示的位置。

当您将其放入RelativeLayout 时,您需要params.addRule(RelativeLayout.CENTER_IN_PARENT)params.addRule(RelativeLayout.CENTER_HORIZONTAL)

于 2013-07-08T08:16:33.193 回答