以编程方式将自定义布局添加到ConstraintLayout

时间:2019-07-01 14:00:52

标签: android android-constraintlayout layout-inflater

大家好。 我正在尝试在ConstraintLayout中的for-loop上布局。

布局如下:

public class SingleMeal extends ConstraintLayout {
    private TextView food_id;
    private ImageButton spacer_minus;
    private ImageButton spacer_plus;
    private TextView food_quantity;

    public SingleMeal(Context context) {
        super(context);
        init();
    }

    private void init(){
        inflate(getContext(), R.layout.activity_single_meal, this);
        this.food_id = findViewById(R.id.food_id);
        this.spacer_minus = findViewById(R.id.spacer_minus);
        this.spacer_plus = findViewById(R.id.spacer_plus);
        this.food_quantity = findViewById(R.id.food_quantity);
    }

    public void setFood_id(String food_id) {
        this.food_id.setText(food_id);
    }

    public TextView getFood_quantity() {
        return food_quantity;
    }

    public ImageButton getSpacer_minus() {
        return spacer_minus;
    }

    public ImageButton getSpacer_plus() {
        return spacer_plus;
    }
}

布局的xml如下:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".selectMeals.SingleMeal">

    <TextView
        android:id="@+id/food_id"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/edit_text_height"
        android:layout_marginStart="@dimen/medium_space"
        android:gravity="center"
        android:textSize="@dimen/text_size"
        android:layout_alignParentStart="true"/>

    <LinearLayout
        android:id="@+id/layout_food_quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/medium_space"
        android:orientation="horizontal"
        android:layout_alignParentEnd="true">

        <ImageButton
            android:id="@+id/spacer_minus"
            android:layout_width="@dimen/plus_minus"
            android:layout_height="@dimen/plus_minus"
            android:background="@drawable/minus"
            android:contentDescription="@string/spacer"/>

        <TextView
            android:id="@+id/food_quantity"
            android:layout_width="@dimen/info_quantity"
            android:layout_height="@dimen/edit_text_height"
            android:gravity="center"
            android:text="0"
            android:singleLine="true"
            android:textSize="@dimen/text_size" />

        <ImageButton
            android:id="@+id/spacer_plus"
            android:layout_width="@dimen/plus_minus"
            android:layout_height="@dimen/plus_minus"
            android:background="@drawable/plus"
            android:contentDescription="@string/spacer"/>
    </LinearLayout>
</RelativeLayout>

在主要活动中,我正在创建一个新的SingleMeal对象,并将其填充为方法。

ConstraintLayout previousCourse = null;
for (Course course : meal.getCourses()) {
    final SingleMeal food_container = new SingleMeal(this);

    food_container.setId(View.generateViewId());
    food_container.setFood_id(course.getName());
    final TextView food_quantity = food_container.getFood_quantity();

    ImageButton minus = food_container.getSpacer_minus();
    minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String foodQuantity = food_quantity.getText().toString();
            int foodQuantityInt = Integer.parseInt(foodQuantity) > 0 ? Integer.parseInt(foodQuantity) - 1 : Integer.parseInt(foodQuantity);
            food_quantity.setText(String.valueOf(foodQuantityInt));
        }
    });

    ImageButton plus = food_container.getSpacer_plus();
    plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String foodQuantity = food_quantity.getText().toString();
            int foodQuantityInt = Integer.parseInt(foodQuantity) + 1;
            food_quantity.setText(String.valueOf(foodQuantityInt));
        }
    });

    lunchCountainer.addView(food_container);

    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(lunchCountainer);
    if (previousCourse == null)
        constraintSet.connect(food_container.getId(), ConstraintSet.TOP , findViewById(R.id.txt_menu_date).getId(), ConstraintSet.BOTTOM,0);
    else
        constraintSet.connect(food_container.getId(), ConstraintSet.TOP , previousCourse.getId(), ConstraintSet.BOTTOM,0);
    constraintSet.applyTo(lunchCountainer);

    previousCourse = food_container;
}

我可能正在做非常错误的事情,因为它通过了循环而没有任何错误,但最终并没有显示任何内容。

如前所述,lunchContainer是ConstraintLayout(在ScrollView内):

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/lunch_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:isScrollContainer="true"
android:orientation="vertical"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

似乎您不需要在这里使用ConstraintLayout,如果您垂直堆叠项目,建议将LinearLayout设置为Vertical。

将每个项目的宽度设置为MATCH_PARENT,然后将每个项目的布局权重设置为相同,但请确保将高度设置为WRAP_CONTENT,以便每个项目的大小均相同。

不能100%地确定这会起作用,但这是这样做的方法。

或者您可以使用ListView,其中包含项目,这还将为您处理滚动等。

这两种选择都对您有用。