2017-07-04 10 views
0

Im Moment habe ich einen RecyclerView und mehrere CardView, um meine Artikel zu zeigen.Android mehrere und dynamische Bilder ausgerichtet

Meine CardView haben eine RelativeLayout, wo ich einige ImageViews hinzufügen (in diesem Fall 3, kann aber mehr sein).

Für jedes meiner Objekte wird eine Liste von Bildern angezeigt, nehmen wir an, # item1 hat Facebook, Twitter und Youtube, aber # item2 nur Facebook- und Youtube-Bilder.

Wie sollte ich die Bilder ausrichten? Wenn ich nur das Twitter-Bild entferne, muss ich layout_toLeftOf meines Facebook-Bildes auf Youtube-Bild aktualisieren.

Ich glaube, ich sollte diese Art von Logik, um alle meine Artikel nicht tun, wird es über Macht ...

Beispiel Bild: https://i.stack.imgur.com/uJZTi.png

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:id="@+id/card_view" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_margin="5dp" 
card_view:cardCornerRadius="4dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/card_height" 
     android:orientation="vertical"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="center_horizontal"> 

      <ImageView 
       android:id="@+id/coverImageView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center" 
       android:scaleType="centerCrop" 
       /> 

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="end|top"> 
       <ImageView android:background="@null" 
        android:id="@+id/facebook" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/facebook_icon" 
        android:layout_toLeftOf="@+id/twitter"/> 
       <ImageView android:background="@null" 
        android:id="@+id/twitter" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/twitter_icon" 
        android:layout_toLeftOf="@+id/youtube"/> 
       <ImageView android:background="@null" 
        android:id="@+id/youtube" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/youtube_icon" 
        android:layout_alignParentRight="true"/> 
      </RelativeLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left|bottom" 
       android:background="@android:drawable/screen_background_dark_transparent" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/titleTextView" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:padding="16dp" 
        android:textSize="@dimen/text_size" 
        android:textColor="#FFFFFF" 
        android:textStyle="bold" /> 
      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 

Antwort

0

Es ist besser um in diesem Fall mit LinearLayout zu arbeiten und layout_alignParentRight und layout_alignParentTop = "true" anstelle von android: layout_gravity = "end | top" wie unten zu verwenden:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true"> 
       <ImageView android:background="@null" 
        android:id="@+id/facebook" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/facebook_icon" 
        android:layout_weight="1"/> 
       <ImageView android:background="@null" 
        android:id="@+id/twitter" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/twitter_icon" 
        android:layout_weight="1"/> 
       <ImageView android:background="@null" 
        android:id="@+id/youtube" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/youtube_icon" 
        android:layout_weight="1"/> 
      </LinearLayout> 
+0

hmm, sieht gut aus! Ich sollte alle ImageViews hinzufügen, die ich für XML haben möchte, und in JAVA arbeiten, um die ImageViews zu löschen, die ich nicht möchte? Da einige Elemente nicht alle Bilder enthalten ... –

+0

können Sie die Sichtbarkeit im Code oder statisch in XML "weg" setzen. (Android: Sichtbarkeit = "weg") Ich sehe dein Problem nicht wirklich – Karim

+0

Ich habe LinearLayout verwendet, wie du mir gesagt hast, aber android: layout_gravity = "end | top" hat nicht funktioniert. Ich behielt die anderen Einstellungen. Ty –

0

Sie sollten LinearLayout mit horizontaler Ausrichtung verwenden!

<LinearLayout 
... 
    android:orientation="horizontal"> 

    //ImageViews in priority order 

</LinearLayout>