gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:09:33 25 4
gpt4 key购买 nike

我正在尝试在彼此下方添加多个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com