在动态添加视图时,Android layout_weight不起作用

时间:2015-01-18 14:51:38

标签: android xml android-layout

我在xml文件中有一行我想要动态地添加到RelativeLayout,但它总是在行的最右上方留下一个空的空格。当我在RelativeLayout父xml中设置此行时,不会留下空白空间。这是我要添加的行xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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" />

这是我用来动态添加行的代码:

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);
        }
    });

任何可能出错的想法?​​

2 个答案:

答案 0 :(得分:1)

将父布局传递到充气机我发现大部分时间都适用于此问题。

新参数是(资源文件,父级,是否要自动添加),我发现最好手动添加

inflate(int resource, ViewGroup root, boolean attachToRoot)

更新onClick

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

答案 1 :(得分:0)

您可以使用以下代码Weight

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearSummeryHead"
   android:orientation="horizontal"
    android:weightSum="230"
    android:background="@color/darkblue"
    android:layout_margin="10dp"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="No"
        android:id="@+id/teextview2"
        android:textColor="@color/white"
        android:layout_weight="40"
        android:textStyle="bold" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Detail"
        android:id="@+id/textView3"
        android:layout_weight="30"
        android:textColor="@color/white"
        android:textStyle="bold"
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Amt"
        android:id="@+id/textView4"
        android:layout_weight="40"
        android:textStyle="bold"
        android:textColor="@color/white"
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Type"
        android:id="@+id/textView5"
        android:layout_weight="40"
        android:textStyle="bold"
        android:textColor="@color/white"
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Date"
        android:id="@+id/textView6"
        android:layout_weight="40"
        android:textStyle="bold"
        android:textColor="@color/white"
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Time"
        android:id="@+id/textView7"
        android:layout_weight="40"
        android:textStyle="bold"
        android:textColor="@color/white"
        />

    </LinearLayout>