2016-10-10 86 views
0

我試圖添加一個帶有EditText元素的新LinearLayout,只要我的應用程序上的按鈕被推入。我有的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> 

我想重複第二個LinearLayout以及它的EditText裏面。 Java代碼我至今是:

public void sendMessage(View view) { 
    LinearLayout layout = new LinearLayout(this); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT); 

    layout.setOrientation(LinearLayout.HORIZONTAL); 
    layout.setWeightSum(1f); 
    params.weight = 1.0f; 


    params.setMargins(0,20,0,0); 

    EditText editText = new EditText(this); 

    editText.setBackgroundResource(R.drawable.my_border); 
    editText.setHint(R.string.edit_message); 
    editText.setLayoutParams(params); 

    LinearLayout container = (LinearLayout)findViewById(R.id.app); 
    container.addView(editText); 

} 

我遇到的問題是,當我按一下按鈕我得到如下:

AppImage

我想要的EditText框與我在xml中定義的高度相同,但它佔用了整個屏幕。

任何想法?

+0

remove * layout.setWeightSum(1f); params.weight = 1.0f; * – Atula

回答

0
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
     LinearLayout.LayoutParams.WRAP_CONTENT); 

在上面的代碼中,你必須設置高度爲match_parent和widht設置爲wrap_content。相反,請嘗試設置高度和寬度以匹配您的xml。高度應爲58dp,寬度應與父級匹配。

希望這會有所幫助!

0

我沒有測試這個時間,但你可以嘗試:

// assuming you have variables for ingredient_list and edit_message 
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ingredientList.getLayoutParams(); 
LinearLayout.LayoutParams editTextParams = (LinearLayout.LayoutParams)editMessage.getLayoutParams(); 

然後,您可以使用這些不是創建參數。

P.S.您在xml中的成分列表中沒有指定的方向。

1

無需在Java中完全重新定義佈局。您應該將其定義爲自己的XML文件。叫它ingredient_input.xml

例如,隨意修改。這是EditText和按鈕的一個「行」,所以是一個水平佈局。也可以是一個RelativeLayout。

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

首先,你需要得到家長的線性佈局

LinearLayout ll = (LinearLayout) findViewById(R.id.ingredient_list); 

然後,您可以擡高在XML文件中的「輸入行」

LayoutInflater inflater = LayoutInflater.from(MainActivity.this); // some context 
View row = inflater.inflate(R.layout.ingredient_input, null); 

您也可以找到按鈕,然後設置其點擊收聽者

row.findViewById(R.id.btnAddMore).setOnClickListener(...); // TODO 

然後,只需添加視圖到父線性佈局

ll.addView(row); 

實際上,你真的可以用一個適配器,而不是一個ListView。

相關問題