LinearLayout添加子布局

  1. 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>
  1. 子布局
<?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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值