通过膨胀布局addView()会导致布局参数失败

时间:2015-10-09 10:29:45

标签: android android-layout

以下XML的预览设计如下所示

enter image description here

XML

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cdvConfirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        card:cardBackgroundColor="#fff"
        card:cardCornerRadius="2dp"
        card:cardElevation="2dp"
        android:focusable="true"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground">

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp">

        <CheckBox
                android:id="@+id/ckbConfirm"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginTop="16dp"
                android:layout_marginRight="10dp"
                android:layout_marginEnd="10dp"
                android:button="@drawable/checkbox_default"
                android:scaleType="fitCenter"

                />

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <TextView
                    android:id="@+id/tvShipmentType"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentLeft="true"
                    android:layout_toLeftOf="@+id/btnDetail"
                    android:layout_toStartOf="@+id/btnDetail"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:text="Title"
                    android:textStyle="bold"
                    android:textColor="@android:color/black"
                    />

            <TextView
                    android:id="@+id/tvShipmentDescription"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentLeft="true"
                    android:layout_toLeftOf="@+id/btnDetail"
                    android:layout_toStartOf="@+id/btnDetail"
                    android:layout_below="@+id/tvShipmentType"
                    android:text="No Appointment Date No Appointment Date No Appointment Date No Appointment Date"
                    android:singleLine="false"
                    android:ellipsize="end"
                    android:textSize="14sp"
                    android:textColor="@android:color/darker_gray"
                    android:textStyle="bold"
                    />

            <TextView
                    android:id="@+id/tvAlert"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tvShipmentDescription"
                    android:layout_marginTop="15dp"
                    android:textColor="#D50000"
                    android:text="** ยังไม่ได้ทำการเลือกรายการนี้"
                    android:visibility="invisible"
                    />

            <Button
                    android:id="@+id/btnDetail"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginLeft="8dp"
                    android:layout_marginStart="8dp"
                    android:padding="10dp"
                    android:text="รายละเอียด"
                    android:background="#81C784"
                    android:src="@drawable/menu_complete"
                    android:scaleType="fitCenter"
                    />

        </RelativeLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

但是当我通过下面的代码通过膨胀XML文件动态地添加View()时,它看起来像这样。似乎toLeftOf无效。

enter image description here

CODE

private void addCardView(int id, String type, String description) {
    View v = View.inflate(this, R.layout.listitem_complete_confirm, null);
    CheckBox ckbConfirm = (CheckBox) v.findViewById(R.id.ckbConfirm);
    TextView tvShipmentType = (TextView) v.findViewById(R.id.tvShipmentType);
    TextView tvShipmentDescription = (TextView) v.findViewById(R.id.tvShipmentDescription);
    TextView tvAlert = (TextView) v.findViewById(R.id.tvAlert);
    Button btnDetail = (Button) v.findViewById(R.id.btnDetail);

    tvShipmentType.setText(type);
    tvShipmentDescription.setText(description);
    btnDetail.setId(id);
    btnDetail.setOnClickListener(this);
    ckbConfirm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                buttonView.setButtonDrawable(R.drawable.checkbox_default);
                tvAlert.setVisibility(View.INVISIBLE);
            }
        }
    });

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
    int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
    params.setMargins(margin, margin, margin, 0);
    llContainer.addView(v, params);
}

我怎样才能解决这个问题,似乎有些价值被忽略了 扩充XML文件并使用addView()。请指教。

3 个答案:

答案 0 :(得分:2)

使用以下内容更改XML文件:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cdvConfirm"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    card:cardBackgroundColor="#fff"
    card:cardCornerRadius="2dp"
    card:cardElevation="2dp"
    android:focusable="true"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:gravity="center_vertical|center_horizontal">

        <CheckBox
            android:id="@+id/ckbConfirm"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:button="@drawable/checkbox_default"
            android:scaleType="fitCenter"

            />


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.70">

                        <TextView
                            android:id="@+id/tvShipmentType"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentLeft="true"
                            android:layout_toLeftOf="@+id/btnDetail"
                            android:layout_toStartOf="@+id/btnDetail"
                            android:paddingTop="5dp"
                            android:paddingBottom="5dp"
                            android:text="Title"
                            android:textStyle="bold"
                            android:textColor="@android:color/black"
                            />

                        <TextView
                            android:id="@+id/tvShipmentDescription"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentLeft="true"
                            android:layout_toLeftOf="@+id/btnDetail"
                            android:layout_toStartOf="@+id/btnDetail"
                            android:layout_below="@+id/tvShipmentType"
                            android:text="No Appointment Date No Appointment Date No Appointment Date No Appointment Date"
                            android:singleLine="false"
                            android:ellipsize="end"
                            android:textSize="14sp"
                            android:textColor="@android:color/darker_gray"
                            android:textStyle="bold"
                            />

                    </LinearLayout>

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.30"
                        android:gravity="center_vertical|center_horizontal">

                        <Button
                            android:id="@+id/btnDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="50dp"
                            android:layout_alignParentEnd="true"
                            android:layout_alignParentRight="true"
                            android:padding="10dp"
                            android:text="รายละเอียด"
                            android:background="#81C784"
                            android:src="@drawable/menu_complete"
                            android:scaleType="fitCenter"
                            />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="15dp">

                    <TextView
                        android:id="@+id/tvAlert"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tvShipmentDescription"
                        android:textColor="#D50000"
                        android:text="** ยังไม่ได้ทำการเลือกรายการนี้"
                        android:visibility="invisible"
                        />
                </LinearLayout>
            </LinearLayout>


        </RelativeLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

这是一个更好的选择,因为我曾经weight代替高度或宽度的数字。

重量就像屏幕上的百分比一样,XML将适应任何屏幕分辨率。

答案 1 :(得分:0)

当使用null参数进行充气时,将忽略布局参数。这就是你膨胀的方式:

View v = View.inflate(this, R.layout.listitem_complete_confirm, null);

使用父容器充气,不要追加。这看起来像这样:

View v = View.inflate(this, R.layout.listitem_complete_confirm, container/parentView, false);

您的容器或父视图必须是ViewGroup。

答案 2 :(得分:0)

您需要更改.xml文件。

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cdvConfirm"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    card:cardBackgroundColor="#fff"
    card:cardCornerRadius="2dp"
    card:cardElevation="2dp"
    android:focusable="true"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground">

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

    <CheckBox
            android:id="@+id/ckbConfirm"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="10dp"
            android:layout_marginEnd="10dp"
            android:button="@drawable/checkbox_default"
            android:scaleType="fitCenter"

            />

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <TextView
                android:id="@+id/tvShipmentType"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/btnDetail"
                android:layout_toStartOf="@+id/btnDetail"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:text="Title"
                android:textStyle="bold"
                android:textColor="@android:color/black"
                />

        <TextView
                android:id="@+id/tvShipmentDescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/btnDetail"
                android:layout_toStartOf="@+id/btnDetail"
                android:layout_below="@+id/tvShipmentType"
                android:layout_marginRight="5dp"
                android:layout_marginEnd="5dp"
                android:text="No Appointment Date No Appointment Date No Appointment Date No Appointment Date"
                android:singleLine="false"
                android:ellipsize="end"
                android:textSize="14sp"
                android:textColor="@android:color/darker_gray"
                android:textStyle="bold"
                />

        <TextView
                android:id="@+id/tvAlert"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tvShipmentDescription"
                android:layout_marginTop="15dp"
                android:textColor="#D50000"
                android:text="** ยังไม่ได้ทำการเลือกรายการนี้"
                android:visibility="invisible"
                />

        <Button
                android:id="@+id/btnDetail"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:padding="10dp"
                android:text="รายละเอียด"
                android:background="#81C784"
                android:src="@drawable/menu_complete"
                android:scaleType="fitCenter"
                />

    </RelativeLayout>

</LinearLayout>