2017-04-18 80 views
0

我创建彼此相邻的一些编辑的文本编程方式使用RelativeLayout的。每个编辑文本的默认宽度都是wrap_content,但是当编辑文本到达屏幕边缘时,它会直观地更改其大小。那么当这种情况发生时,我该如何让它移动到下一行呢?如何在android中以编程方式更改编辑文本的位置?

private EditText createEditText(EditText editText1, EditText editText2, String word){ 
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
      editText.getLayoutParams().width, 
      editText.getLayoutParams().height 
    ); 

    layoutParams.addRule(RelativeLayout.RIGHT_OF, editText1.getId()); 
    layoutParams.addRule(RelativeLayout.BELOW, textView.getId()); 

    layoutParams.leftMargin += 60; 
    editText2.setHint("" + word); 
    editText2.setHintTextColor(editText.getSolidColor()); 
    editText2.setBackgroundResource(R.drawable.rounded_edittext); 
    editText2.setLayoutParams(layoutParams); 
    editText2.setPadding(editText.getPaddingLeft(), editText.getPaddingTop(), editText.getPaddingRight(), editText.getPaddingBottom()); 
    editText2.setId(View.generateViewId()); 

    relativeLayout.addView(editText2); 

    return editText2; 
} 
+0

使用的LinearLayout。 – Sergey

+0

@Sergey,但我需要在我的项目RelativeLayout ... –

+0

我不认为使用RelativeLayout你实现你想要的,bcz我看到规则RelativeLayout.BELOW它总是会低于你的TextView。 – Sergey

回答

0

你可以试试。不知道,如果它能工作。 你可以做的就是计算屏幕上的onCreate宽度和如果视图超过,让下面的最后一个新的相对进行检查。 说实话,线性布局将更容易维护使用重量属性。

1

尝试的FlowLayout在Android和dynamiaclly膨胀TextViews在里面。

而不是你可以使用Linearlayout与每行中的固定数量的孩子,如果这样做,孩子们必须具有相同的layout_weight。

但不是在这么大的争议会我会要求你简单地与水平方向的,而不是替代的RelativeLayout RelativeLayout的随着的FlowLayout在android系统

相关问题