下面的布局参数不能以编程方式工作

时间:2016-03-22 21:01:06

标签: android android-linearlayout android-cardview layoutparams

我正在尝试添加规则以将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。
所以我试图用Java转换这个XML,因为我有很多要显示的类别。我确实喜欢这个:

    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);
    }

这会创建很多具有良好定位类别名称(textView)的CardView,但我无法定位类别描述。这行不会对textView1产生任何影响(对应于类别描述):

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

我不明白,因为类别名称定位良好,我使用了相同的技术:addRule。
我忘记了什么或弄错了吗?
这是一个显示两个结果的picture 提前谢谢。

1 个答案:

答案 0 :(得分:2)

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

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

但是,自从您从代码创建TextView后,它没有分配任何ID。在XML中,TextView的标识为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);
}