2017-07-04 51 views
0

現在,我有一個RecyclerView和多個CardView來顯示我的項目。Android多個和動態圖像對齊

我的CardView有一個RelativeLayout,我添加了一些ImageViews(本例中是3,但可以更多)。

我的每件物品都會顯示圖片列表,假設#item1有Facebook,Twitter和Youtube,但#item2只有Facebook和Youtube圖片。

我應該如何對齊圖像?如果我只是刪除Twitter圖片,我需要將我的Facebook圖片的layout_toLeftOf更新爲Youtube圖像。

我想我不應該做這樣的邏輯來我的所有項目,這將是對權力......

示例圖片: 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> 

回答

0

這是更好在這種情況下使用LinearLayout並使用layout_alignParentRight和layout_alignParentTop =「true」而不是android:layout_gravity =「end | top」,如下所示:

<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

嗯,看起來不錯!我應該添加我想要的所有ImageView,並使用JAVA來刪除我不想要的ImageViews?因爲有些項目沒有全部圖像... –

+0

您可以將代碼中的可見性設置爲「不見了」或者在xml中靜態設置。 (android:visibility =「gone」)我真的沒有看到你的問題 – Karim

+0

我用LinearLayout,就像你告訴我的,但android:layout_gravity =「end | top」不起作用。我保留了其他設置。 Ty –

0

您應該使用LinearLayout水平方向!

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

    //ImageViews in priority order 

</LinearLayout>