2017-04-18 149 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系統

相關問題