以编程方式将项添加到CardView

时间:2018-04-09 11:10:22

标签: android android-cardview android-tablelayout

我有以下XML

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="2dp"
                app:cardBackgroundColor="@android:color/white"
                app:cardElevation="2dp"
                app:cardUseCompatPadding="true">

                    <LinearLayout
                        android:background="@drawable/border_bottom"
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:background="@drawable/round"
                            android:id="@+id/image"
                            android:scaleType="fitXY"
                            android:adjustViewBounds="true"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:padding="2dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginLeft="5dp"/>

                        <TextView
                            android:gravity="center|start"
                            android:textStyle="bold"
                            android:text=""
                            android:textSize="16sp"
                            android:id="@+id/tv_billtype"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:paddingBottom="3dp"/>
                    </LinearLayout>

                    <TableLayout
                        android:background="@drawable/border_bottom"
                        android:id="@+id/table_items"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
            </android.support.v7.widget.CardView>

        </LinearLayout>
    </ScrollView>

正如您所见,TableLayout中有LinearLayoutCardView。当我以编程方式在TableLayout中添加一行时,LinearLayout低于TableLayout,我看不到LinearLayout。
它像FrameLayout一样工作。当我从我的代码中删除CardView时,它可以正常工作。

 TableLayout tableLayout = (TableLayout)findViewById(R.id.table_items);
    tableLayout.invalidate();
    tableLayout.removeAllViews();
    for(int i = 0; i < array.length(); i++) {
        TableRow trFinish = (TableRow) LayoutInflater.from(this).inflate(R.layout.row_billdetail_product, null);
        TextView name, price, quantity;
        name = (TextView)trFinish.findViewById(R.id.tv_name);
        price = (TextView)trFinish.findViewById(R.id.tv_price);
        quantity = (TextView)trFinish.findViewById(R.id.tv_quantity);

        JSONObject obj = null;
        obj = array.getJSONObject(i);
        name.setText(obj.getString("Name"));
        price.setText(obj.getString("Price"));
        quantity.setText(obj.getString("Qty") + " x " + obj.getString("PriceUnit"));

        tableLayout.addView(trFinish);
    }

如何正确添加?

我的整个XML。虽然没有完成。

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="2dp"
                app:cardBackgroundColor="@android:color/white"
                app:cardElevation="2dp"
                app:cardUseCompatPadding="true">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:background="@drawable/border_bottom"
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <ImageView
                            android:background="@drawable/round"
                            android:id="@+id/image"
                            android:scaleType="fitXY"
                            android:adjustViewBounds="true"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:padding="2dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginLeft="5dp"/>

                        <TextView
                            android:gravity="center|start"
                            android:textStyle="bold"
                            android:text=""
                            android:textSize="16sp"
                            android:id="@+id/tv_billtype"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:paddingBottom="3dp"/>
                    </LinearLayout>

                    <TableLayout
                        android:background="@drawable/border_bottom"
                        android:id="@+id/table_items"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/> 

                    <TableLayout
                        android:background="@drawable/border_bottom"
                        android:id="@+id/table_prices"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>

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

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="2dp"
                app:cardBackgroundColor="@android:color/white"
                app:cardElevation="2dp"
                app:cardUseCompatPadding="true">

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

                        <TextView
                            android:id="@+id/note_title"
                            android:scaleType="fitXY"
                            android:adjustViewBounds="true"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:padding="2dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginLeft="5dp"/>

                        <TextView
                            android:gravity="center|start"
                            android:textStyle="bold"
                            android:text=""
                            android:textSize="16sp"
                            android:id="@+id/note_contents"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:paddingBottom="3dp"/>


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

        </LinearLayout>
    </ScrollView>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <!--there is a two button-->
        </LinearLayout>

    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

试试这个xml,让我知道它是否有效

<serviceTask id="anExternalServiceTask"
   camunda:type="external"
   camunda:topic="ShipmentProcessing" />