- xml布局(定义了一个LinearLayout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/add_linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
- 子布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FAFAFA"
android:layout_marginBottom="1dp"
android:layout_width="match_parent"
android:layout_height="65dp">
<ImageView
android:src="@drawable/banana"
android:id="@+id/produce_image"
android:scaleType="fitXY"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:layout_width="100dp"
android:layout_height="65dp" />
<LinearLayout
android:layout_marginTop="8dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_marginLeft="10dp"
android:layout_marginBottom="2dp"
android:id="@+id/produce_name"
android:gravity="center"
android:textSize="18sp"
android:text="苹果"
android:textColor="#66BB6A"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"/>
<LinearLayout
android:layout_marginBottom="2dp"
android:layout_marginLeft="10dp"
android:layout_height="0dp"
android:layout_width="wrap_content"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/produce_price"
android:text="3.5"
android:textColor="#EF5350"
android:textSize="18sp"/>
<TextView
android:id="@+id/produceQuantity_text"
android:layout_marginLeft="20dp"
android:textColor="#66BB6A"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="X 0.5"
android:textSize="18sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
3.逻辑代码
View view = LayoutInflater.from(mContext).inflate(R.layout.produce_order_item, null);
ImageView produceImage = (ImageView) view.findViewById(R.id.produce_image);
TextView produceName = (TextView) view.findViewById(R.id.produce_name);
TextView producePrice = (TextView) view.findViewById(R.id.produce_price);
TextView produceQuantity = (TextView) view.findViewById(R.id.produceQuantity_text);
Glide.with(mContext).load(tempUrl).into(produceImage);
produceName.setText(produce.getName());
producePrice.setText("¥" + String.valueOf(produce.getPrice()));
produceQuantity.setText("× " + String.valueOf(produces.getQuantity()));
View line = LayoutInflater.from(mContext).inflate(R.layout.line, null);
addLinearLayout.addView(view);
addLinearLayout.addView(line);