繁体   English   中英

Android:布局没有互相添加

[英]Android: Layouts not being added below each other

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

问题 :如何让ConstraintLayouts低于对方?

现在有什么乐趣:

例1:

在此输入图像描述

例2:

在此输入图像描述

它应该如何:

在此输入图像描述

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>

您应该使用LinearLayout进行布局评论,方向垂直。

编辑:

使用链接 attibute

你能试一下吗 ?

<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"
            app:layout_constraintVertical_chainStyle="spread" > 
</RelativeLayout>

希望这可以帮助。

这是因为根(父)布局是RelativeLayout而不是LinearLayout。

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

将它们添加到具有垂直方向的LinearLayout中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM