Android layout_weight doesn't work when adding a view dynamically



I have a row in an xml file that I would like to add to a RelativeLayout dynamically, but it always leaves an empty space at the uppermost right of the row. When I set this row in its RelativeLayout parent xml, no empty space is left. Here's the row xml I am trying to add:



<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/food_nutrients_line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >

<TextView
android:id="@+id/food"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_no_gradient_brown_up_stroke"
android:padding="3dp"
android:singleLine="true"
android:text="name"
android:textColor="@android:color/holo_green_dark"
android:textSize="10sp" />

<TextView
android:id="@+id/units"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/edittext_no_gradient_brown_up_stroke"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="un"
android:textColor="@android:color/holo_green_dark"
android:textSize="10sp" />

<EditText
android:id="@+id/quantity"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_no_gradient_brown_up_stroke"
android:gravity="center"
android:hint="@string/quantity_edittext"
android:inputType="number|numberDecimal"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:textColor="@android:color/holo_green_dark"
android:textSize="10sp" />

<TextView
android:id="@+id/kcal"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_no_gradient_brown_up_stroke"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="4"
android:textColor="@android:color/holo_green_dark"
android:textSize="10sp" />

<TextView
android:id="@+id/carbs"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_no_gradient_brown_up_stroke"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:textColor="@android:color/holo_green_dark"
android:textSize="10sp" />

<TextView
android:id="@+id/proteins"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_no_gradient_brown_up_stroke"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:textColor="@android:color/holo_green_dark"
android:textSize="10sp" />

<TextView
android:id="@+id/fat"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_no_gradient_brown_up_stroke"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="0"
android:textColor="@android:color/holo_green_dark"
android:textSize="10sp" />


And here's the code I use to dynamically add the row:



plusButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View foodLine = inflater.inflate(R.layout.food_line, null);
LinearLayout blank = (LinearLayout) findViewById(R.id.food_nutrients_blank_line);
blank.addView(foodLine);
}
});


Any ideas of what may be wrong?


No comments:

Post a Comment