0

因此,我正嘗試以編程方式將EditText添加到LinearLayout。我在其上點擊了一個'+'ImageButton,我將添加一個EditText,直到有五個。以下是我的XML文件的片段。以編程方式添加edittext時,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> 

LinearLayout ID爲 「容器」 將作爲的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

嘗試將android: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"> 

我認爲這會幫助您。

相關問題