0

Так что я пытаюсь добавить EditText в LinearLayout программно. Я добавил «+» ImageButton, на клик которого я добавлю EditText до пяти. Ниже приведен фрагмент моего XML-файла.LinearLayout не расширяется при добавлении edittext программно

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/margin_16" 
      android:orientation="horizontal"> 

      <LinearLayout 
       android:id="@+id/container" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:orientation="vertical"> 



      </LinearLayout> 

      <ImageButton 
       android:id="@+id/add_field" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_marginBottom="@dimen/margin_16" 
       android:background="@drawable/cyan_top_rounded" 
       android:src="@drawable/add" /> 
     </LinearLayout> 

LinearLayout с идентификатором "Контейнер" будет иметь EditText, как ребенок. Ниже приведен мой код для добавления EditText.

mAddField.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (count < 5) { 
       mAddField.setEnabled(true); 
       mContainer.addView(getTextField()); 
       count++; 
      } else { 
       mAddField.setEnabled(false); 
      } 
     } 
    }); 

private EditText getTextField() { 

    EditText et = new EditText(this); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    et.setLayoutParams(params); 
    et.setText("abcd"); 

    return et; 
} 

private void initUI() { 
    mAddField = (ImageButton) findViewById(R.id.add_field); 
    mContainer = (LinearLayout) findViewById(R.id.container); 

    EditText et = new EditText(this); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    et.setLayoutParams(params); 
    et.setText("abcd"); 
    mContainer.addView(et); 
} 

Однако, когда я нажимаю на ImageButton, ничего не происходит. Если EditText добавлен вообще, LinearLayout также не расширяется. Пожалуйста помоги.

+0

попробуйте изменить андроид: layout_height = "match_parent" для android: layout_height = "wrap_content" –

+0

Удалить вес и установить высоту для wrap_content & check. –

+0

да этот один работал. Я установил высоту обоих LinearLayouts для wrap_content. Спасибо, мужчины –

ответ

1

Попробуйте это:

... 
      <LinearLayout 
       android:id="@+id/container" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:orientation="vertical"> 



      </LinearLayout> 
... 
1
Change ur layout to this it will work fine:(Make the necessary changes according to ur own). Problem is with the height and width of the outer as well as the inner LinearLayout. 

<LinearLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical"> 
    </LinearLayout> 

    <ImageButton 
     android:id="@+id/add_field" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/image" /> 
</LinearLayout> 
0

мнение, в котором вы, что добавить EditText динамически, вы должны установить его ширину и высоту, как «wrap_content». Поэтому просто измените свой внутренний макет на:

<LinearLayout 
       android:id="@+id/container" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:orientation="vertical"> 

Я думаю, что это поможет вам.

Смежные вопросы