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

我认为这会帮助您。

相关问题