2017-03-22 1 views
3

Ich versuche, mehrere ConstraintLayouts untereinander hinzuzufügen. Aber sie scheinen auf der gleichen Position zu sein. (Siehe Bild).Android: Layouts werden nicht untereinander hinzugefügt

Frage: Wie bekomme ich die ConstraintLayouts unter einander?

Was happpening jetzt:

Beispiel 1:

enter image description here

Beispiel 2:

enter image description here

Wie es sollte wie folgt aussehen:

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 (Die Sache, die ich einfügen bin versucht)

<?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 die alle Bewertungen enthalten wird.

<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> 

Antwort

1

Das passiert, weil das Stammlayout RelativeLayout anstelle von LinearLayout ist.

In einem RelativeLayout hinzugefügte untergeordnete Ansichten positionieren sich nicht nacheinander, da ihre übergeordneten Elemente ihre Positionierung in Abhängigkeit von ihren untergeordneten Attributen festlegen (weshalb die Positionierung relativ ist).

Fügen Sie sie stattdessen in einem LinearLayout mit vertikaler Ausrichtung hinzu.

+0

Das funktionierte für mich, vielen Dank :) – Guido

1

Sie sollten ein LinearLayout für Ihre Layout-Reviews verwenden, mit einer vertikalen Ausrichtung.

EDIT:

Verwenden Chains attibute

Können Sie versuchen?

<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> 

Hoffe, das hilft.

+0

Ist es nicht möglich, dies mit ConstraintLayouts zu tun? – Guido

+0

Mein Fehler, sehen, ob meine Bearbeitung hilfreich ist – Cochi

+0

Danke für Ihre Antwort, aber ich denke, Verkettung ist Teil von CoordinatorLayout, und Sie setzen es zwischen RelativeLayout-Tags. – Guido

Verwandte Themen