2017-07-24 4 views
1

Ich habe ein LinearLayout mit der Ausrichtung horizontal, aber wenn es 4 Elemente in einer Zeile kreuzt fügt es horizontal hinzu, ich möchte es automatisch in die nächste Zeile ändern, Beispiel:Bild in der nächsten Zeile in LinearLayout hinzufügen Programatcally - Android

Was geschieht: Whta happen:

Was ich will: What I want:

Mein Code:

btnAdd.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      countLikesAdd++; 
      removeMessageOrShow(); 
      ImageView iconLike = new ImageView(Register30.this); 
      iconLike.setImageDrawable(getDrawable(R.drawable.musicicon)); 
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(140,130); 
      lp.setMargins(5,5,5,5); 
      iconLike.setLayoutParams(lp); 
      relativeLayout.addView(iconLike); 
     } 
    }); 

Meine XML:

<ScrollView 
    android:id="@+id/svLikes" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_marginBottom="60dp" 
    android:layout_marginEnd="14dp" 
    android:layout_marginStart="14dp" 
    android:layout_marginTop="48dp" 
    android:background="#2f3342" 
    android:orientation="vertical" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="@+id/btnAdd" 
    app:layout_constraintVertical_bias="0.0" 
    tools:layout_constraintBottom_creator="1" 
    tools:layout_constraintLeft_creator="1" 
    tools:layout_constraintRight_creator="1" 
    tools:layout_constraintTop_creator="1"> 
    <LinearLayout 
     android:id="@+id/linearLayout" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="5dp" 
     android:paddingEnd="5dp" 
     android:paddingTop="5dp" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     > 
    </LinearLayout> 
</ScrollView> 

Antwort

0

LinearLayout nicht das Konzept der Verpackung in der nächsten Zeile nicht unterstützt, wenn etwas nicht passen würde.

Für eine kleine Anzahl fester Objekte ist es wahrscheinlich die beste Lösung, eine der vielen "FlowLayout" -Bibliotheken zu verwenden, wenn Sie eine Google-Suche durchführen.

Für eine große Anzahl von Elementen (oder Elementen, die Sie zur Entwurfszeit nicht kennen) können Sie RecyclerView mit einer GridLayoutManager verwenden.

+0

Rechts. Ich habe eine gute Bibliothek dafür gefunden. Danke für die Antwort! – Adolfok3

+2

Betrachten Sie [FlexboxLayout] (https://github.com/google/flexbox-layout) - es kann sowohl für das manuelle Layout einer kleinen Anzahl von Elementen verwendet werden als auch in RecyclerView eingebunden werden. – ephemient

Verwandte Themen