0

EditText를 프로그래밍 방식으로 LinearLayout에 추가하려고합니다. 나는 누구의 클릭에 '+'ImageButton을 추가했는데, 5 시까 지 EditText을 추가 할 것입니다. 아래는 내 XML 파일의 발췌 문장입니다. 프로그래밍 방식으로 편집 텍스트를 추가 할 때 LinearLayout이 확장되지 않습니다.

<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> 

는 ID가 "컨테이너"와 LinearLayout는 자식으로 글고있을 것이다. 다음은 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

android : layout_height = "match_parent"를 android : layout_height = "wrap_content"로 변경해보십시오. –

+0

wrap_content 및 확인하려면 체중 및 높이를 제거하고 확인하십시오. –

+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"> 

이것이 도움이 될 것입니다.