java - Android:布局未添加到彼此下方

标签 java android android-layout

我正在尝试在彼此下方添加多个 ConstraintLayout。但他们似乎处于相同的位置。 (见图)。

问题:如何让 ConstraintLayouts 彼此低于彼此?

现在发生了什么:

示例 1:

enter image description here

示例 2:

enter image description here

它应该是什么样子:

enter image description here

Activity .java

String response = (String) databaseManager.execute(phpLocation,  parameters).get();
        List<Review> listOfReviews = (List<Review>) EntityConverter.getInstance().jsonToObject(response,
                new TypeToken<Collection<Review>>(){}.getType());

        int totalscore = 0;
        int amountOfReviews = 0;

        RelativeLayout relativeLayoutReviews = (RelativeLayout) findViewById(R.id.relativeLayoutReviews);
        for(Review r : listOfReviews) {
            LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = vi.inflate(R.layout.reviewtemplate, null);
            v.setId(amountOfReviews);

            TextView name = (TextView) v.findViewById(R.id.textViewReviewName);
            name.setText(r.getName());

            RatingBar ratingBar = (RatingBar) v.findViewById(R.id.ratingBarReviewScore);
            ratingBar.setRating(r.getScore());
            totalscore += r.getScore();

            TextView ratingText = (TextView) v.findViewById(R.id.textViewReviewText);
            ratingText.setText(r.getReviewtext());

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

            if (amountOfReviews != 0) {
                // add the rule that places your button below your EditText object
                params.addRule(RelativeLayout.BELOW, v.getId() -1);
            }

            relativeLayoutReviews.addView(v, amountOfReviews);
            amountOfReviews++;
        }

reviewtemplate.xml(我要插入的东西)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/reviewTemplate"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textViewReviewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Pietje"
        android:textSize="24sp"
        android:textColor="#000000"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent" />

    <RatingBar
        android:id="@+id/ratingBarReviewScore"
        android:layout_width="243dp"
        android:layout_height="58dp"
        android:numStars="5"
        android:rating="0"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/textViewReviewName"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/textViewReviewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Text"
        android:textColor="#000000"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ratingBarReviewScore" />
</android.support.constraint.ConstraintLayout>

将包含所有评论的 RelativeLayout。

<RelativeLayout
            android:id="@+id/relativeLayoutReviews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="1dp"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"> 
</RelativeLayout>

最佳答案

发生这种情况是因为根(父)布局是 RelativeLayout 而不是 LinearLayout。

添加到 RelativeLayout 中的 subview 不会一个接一个地定位,因为它们的父 View 根据它们的子属性布置它们的定位(因此定位是相对的)。

改为将它们添加到垂直方向的 LinearLayout 中。

关于java - Android:布局未添加到彼此下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42955229/

相关文章:

android - android布局xml中的background、backgroundTint、backgroundTintMode属性有什么区别?

android - 如何设置android :layout_column programmatically

java - 无法使用 IP 地址连接到 MySQL 服务器

android - 将 Unity 与 Eclipse 集成 - 如何按照 "official"教程的说明进行操作?

java - 跟踪 GPS 位置 - 实时反馈

android - DataBaseHelper,是否单例?

java - 更改选定微调项的文本大小

java - 使用 GAE/Java 运行 DataNucleus JPA 时出现问题

java - Web服务调用时如何拦截xml响应和请求?

java - Action into Submenu 上下文菜单 Java JFace SWT Eclipse