下面的布局参数无法以编程方式工作

Layout Params below doesn't work programmatically

我正在尝试在下方添加规则以将一个 TextView 置于另一个下方,但我无法以编程方式执行此操作。 我有一个像这样的 XML :

        <android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            card_view:cardCornerRadius="4dp"
            card_view:cardBackgroundColor="#00bcd4">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin">

                <TextView
                    android:id="@+id/info_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Category Name"
                    style="@style/Base.TextAppearance.AppCompat.Title"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:layout_centerHorizontal="true" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Vel voluptatem soluta ipsa. Voluptatem est quod non explicabo aut quisquam quas. Voluptatem aliquam iure voluptas"
                    android:id="@+id/textView"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:layout_below="@+id/info_text"
                    android:layout_above="@+id/button"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Help"
                    android:id="@+id/button"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Play"
                    android:id="@+id/button2"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentStart="true" />

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

    </LinearLayout>

这很好用!我得到了我想要的结果:一个带有类别名称、描述和 2 个按钮的 CardView。
所以我尝试将这个 XML 转置到 Java 中,因为我有很多类别要显示。我喜欢这个:

    LinearLayout linearLayoutCategory = (LinearLayout) findViewById(R.id.linearLayoutCategory);
    for (int i = 0; i < categories.size(); i++) {
        Category category = categories.get(i);
        CardView cardView = new CardView(this);
        cardView.setCardBackgroundColor(Color.parseColor("#00bcd4"));
        cardView.setRadius((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics()));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics()), Gravity.CENTER);
        layoutParams.setMargins(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()), 0, 0);
        cardView.setLayoutParams(layoutParams);

        RelativeLayout relativeLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(layoutParams1);

        TextView textView = new TextView(this);
        RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams2.addRule(RelativeLayout.CENTER_HORIZONTAL);
        textView.setLayoutParams(layoutParams2);
        textView.setText(category.getName());
        textView.setTextAppearance(this, android.R.style.TextAppearance_Large);
        textView.setId(i);
        relativeLayout.addView(textView);

        TextView textView1 = new TextView(this);
        RelativeLayout.LayoutParams layoutParams3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layoutParams3.addRule(RelativeLayout.BELOW, textView.getId());
        textView1.setLayoutParams(layoutParams3);
        textView1.setText(category.getDescription());
        relativeLayout.addView(textView1);

        Button button = new Button(this);
        relativeLayout.addView(button);

        Button button1 = new Button(this);
        relativeLayout.addView(button1);

        cardView.addView(relativeLayout);
        linearLayoutCategory.addView(cardView);
    }

这会创建很多 CardView,它们具有定位良好的类别名称 (textView),但我无法定位类别描述。这一行看起来对textView1没有任何影响(对应类别描述):

     layoutParams3.addRule(RelativeLayout.BELOW, textView.getId());

我不明白,因为类别名称的位置很好,而且我使用了相同的技术:addRule。
我忘记了什么或犯了错误吗?
这是一个 picture 向您展示两个结果
提前致谢。

问题是您的规则基于 TextView id:

layoutParams3.addRule(RelativeLayout.BELOW, textView.getId());

但是由于您是通过代码创建 TextView,所以它没有分配任何 ID。在 XML 中你的 TextView 有 id android:id="@+id/info_text" 这就是它在 XML.

中工作正常的原因

你真的不需要将你的 XML 布局转换成代码 - 你需要的 - 只是为每个类别扩展你的布局并将结果添加到容器(我相信你使用垂直 LinearLayout 作为类别的容器):

main_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/linearLayoutCategory"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

category.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        card_view:cardCornerRadius="4dp"
        card_view:cardBackgroundColor="#00bcd4">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

            <TextView
                android:id="@+id/info_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Category Name"
                style="@style/Base.TextAppearance.AppCompat.Title"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Vel voluptatem soluta ipsa. Voluptatem est quod non explicabo aut quisquam quas. Voluptatem aliquam iure voluptas"
                android:id="@+id/textView"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:layout_below="@+id/info_text"
                android:layout_above="@+id/button"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Help"
                android:id="@+id/button"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Play"
                android:id="@+id/button2"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true" />

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

业务逻辑:

LinearLayout linearLayoutCategory = (LinearLayout)findViewById(R.id.linearLayoutCategory);
for (int i = 0; i < categories.size(); i++) {
    View category = getLayoutInflater().inflate(R.layout.category, linearLayoutCategory, false);

    TextView infoText = category.findViewById(R.id.info_text);
    infoText.setText(/*category name?*/);

    linearLayoutCategory.addView(category);
}