Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式将自定义布局添加到ConstraintLayout_Android_Android Constraintlayout_Layout Inflater - Fatal编程技术网

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

Android 以编程方式将自定义布局添加到ConstraintLayout,android,android-constraintlayout,layout-inflater,Android,Android Constraintlayout,Layout Inflater,大家好。 我正在尝试在for循环中将布局设置为ConstraintLayout 布局贴花如下所示: public class SingleMeal extends ConstraintLayout { private TextView food_id; private ImageButton spacer_minus; private ImageButton spacer_plus; private TextView food_quantity; pub

大家好。 我正在尝试在
for循环
中将布局设置为
ConstraintLayout

布局贴花如下所示:

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;
    }
}
<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>
布局的xml如下所示:

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;
    }
}
<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>
我可能做了一些非常错误的事情,因为它在循环中没有出现任何错误,但最终没有显示任何内容

如前所述,
午餐容器
是一个约束窗口(在
滚动视图
中):



有什么想法吗?

似乎您不希望在此处显示约束,如果您垂直堆叠项目,我建议将LinearLayout设置为垂直

将每个项的宽度设置为与父项匹配,然后将每个项的布局权重设置为相同,但确保将高度设置为“包裹内容”,以便每个项的大小相同

不是100%确定这会起作用,但这是一种方法

或者你可以使用一个列表视图,其中包含你的项目,这也将为你处理滚动等


这两个选项都适合您。

您似乎不希望在此处显示约束,如果您垂直堆叠项目,我建议将LinearLayout设置为垂直

将每个项的宽度设置为与父项匹配,然后将每个项的布局权重设置为相同,但确保将高度设置为“包裹内容”,以便每个项的大小相同

不是100%确定这会起作用,但这是一种方法

或者你可以使用一个列表视图,其中包含你的项目,这也将为你处理滚动等


任何一种选择都适用。

我非常确定您应该查看您案例的
RecyclerView
而不是
ConstraintLayout
。我非常确定您应该查看您案例的
RecyclerView
而不是
ConstraintLayout
。。。