Android 以编程方式添加线性布局

Android 以编程方式添加线性布局,android,android-layout,Android,Android Layout,每当按下应用程序上的按钮时,我都会尝试添加一个新的LinearLayout,其中包含EditText元素。我拥有的xml是 <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/app" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com

每当按下应用程序上的按钮时,我都会尝试添加一个新的LinearLayout,其中包含EditText元素。我拥有的xml是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/app"
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:weightSum="1">
   <EditText android:id="@+id/dish_name"
      android:layout_width="match_parent"
      android:layout_height="58dp"
      android:hint="@string/dish_name"
      android:background="@drawable/my_border"
      android:paddingLeft="10dip"/>
      <LinearLayout android:id="@+id/ingredient_list"
         android:layout_width="match_parent"
         android:layout_height="58dp"
         android:weightSum="1"
         android:layout_marginTop="20dp">
         <EditText android:id="@+id/edit_message"
             android:layout_width="0dp"
             android:layout_height="42dp"
             android:hint="@string/edit_message"
             android:background="@drawable/my_border"
             android:layout_weight="1"
             android:paddingLeft="10dip"/>
         <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/button_send"
             android:onClick="sendMessage" />
       </LinearLayout>
</LinearLayout>
我遇到的问题是,当我单击按钮时,我会看到以下内容:

我希望EditText框的高度与我在xml中定义的高度相同,但它占据整个屏幕

有什么想法吗

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
在上面的代码中,高度设置为
match\u parent
,宽度设置为
wrap\u content
。相反,请尝试设置高度和宽度以匹配xml。高度应为
58dp
,宽度应与父级匹配


希望这有帮助

我没有时间测试这个,但你可以试试:

// assuming you have variables for ingredient_list and edit_message
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ingredientList.getLayoutParams();
LinearLayout.LayoutParams editTextParams = (LinearLayout.LayoutParams)editMessage.getLayoutParams();
然后可以使用这些参数,而不是创建参数


另外,在xml的成分列表中没有指定的方向。

无需完全在Java中重新定义布局。您应该将其定义为自己的XML文件。称之为
component\u input.xml

例如,可以随意修改。这是编辑文本和按钮的一行,因此是水平布局。也可能是相对的

  <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="58dp"
     android:weightSum="1"
     android:orientation="horizontal"
     android:layout_marginTop="20dp">
     <EditText android:id="@+id/edit_message"
         android:layout_width="0dp"
         android:layout_height="42dp"
         android:hint="@string/edit_message"
         android:background="@drawable/my_border"
         android:layout_weight="1"
         android:paddingLeft="10dip"/>
     <Button
         android:id="@+id/btnAddMore"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/button_send"/>
   </LinearLayout>
然后,可以为“输入行”膨胀XML文件

您还可以找到该按钮,然后设置其单击侦听器

row.findViewById(R.id.btnAddMore).setOnClickListener(...); // TODO
然后,只需将该视图添加到父线性布局

LinearLayout ll = (LinearLayout) findViewById(R.id.ingredient_list);
ll.addView(row);
实际上,您实际上可以使用带有适配器的ListView

移除布局。设置权重总和(1f);参数重量=1.0f;
row.findViewById(R.id.btnAddMore).setOnClickListener(...); // TODO
ll.addView(row);