0

最近我一直試圖在多個水平RecyclerView內部實現CardView。但是,我的CardView似乎佔用了整個屏幕的寬度,因爲ImageView中的圖像與我指定的大小不完全相同。我知道這一點,因爲當我放入與寬度尺寸相匹配的圖像時,它完全按照我想要的方式工作。但我希望程序能夠裁剪圖像而不管尺寸。 What I Want這裏是what I get當使用的圖像不是我的XML文件中指定的寬度相同的寬度。CardView中的ImageView佔用整個屏幕

這是我在我的recyclerview中使用的cardview。

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:background="@android:color/white" 
 
    android:orientation="vertical" android:layout_width="match_parent" 
 
    android:layout_height="wrap_content"> 
 

 

 
    <RelativeLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent"> 
 

 
     <TextView 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:id="@+id/txtRecipeSectionTitle" 
 
      android:text="Recipe Section" 
 
      android:textStyle="bold" 
 
      android:textSize="14sp" 
 
      android:padding="5dp" 
 
      android:textColor="@android:color/black" 
 
      android:layout_marginLeft="30dp" 
 
      android:layout_marginTop="10dp" 
 
      tools:layout_editor_absoluteY="0dp" 
 
      app:layout_constraintLeft_toLeftOf="parent" /> 
 

 

 
    </RelativeLayout> 
 

 
    <android.support.v7.widget.RecyclerView 
 
     android:id="@+id/recipe_section_recycler_view" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="242dp" 
 
     android:padding="3dp" 
 
     android:layout_marginLeft="25dp" 
 
     android:layout_marginTop="5dp" 
 
     android:orientation="horizontal"></android.support.v7.widget.RecyclerView> 
 

 
</LinearLayout>

這裏是我的recyclerview的XML。雖然我認爲這不是問題。

<?xml version="1.0" encoding="utf-8"?> 
 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="133dp" 
 
    android:layout_height="242dp" 
 
    app:cardElevation="0dp" 
 
    android:orientation="horizontal"> 
 

 
    <RelativeLayout 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="fill_parent" 
 
     android:background="?android:selectableItemBackground" 
 
     android:orientation="vertical" 
 
     android:layout_marginLeft="10dp" 
 
     tools:layout_editor_absoluteX="0dp" 
 
     tools:layout_editor_absoluteY="0dp"> 
 

 
     <ImageView 
 
      android:id="@+id/recipeThumbnail" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="195dp" 
 
      android:contentDescription="" 
 
      android:src="@drawable/placeholder" 
 
      android:scaleType="centerCrop" 
 
      tools:layout_editor_absoluteX="0dp" 
 
      tools:layout_editor_absoluteY="0dp" /> 
 

 
     <TextView 
 
      android:id="@+id/txtRecipeName" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:layout_below="@+id/recipeThumbnail" 
 
      android:text="Name of Recipe" 
 
      android:textColor="@android:color/black" 
 
      android:textSize="14sp" 
 
      tools:layout_editor_absoluteX="0dp" 
 
      tools:layout_editor_absoluteY="0dp" /> 
 

 
     <TextView 
 
      android:id="@+id/txtCalorie" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_below="@+id/txtRecipeName" 
 
      android:text="Calories: " 
 
      android:textColor="@android:color/black" 
 
      android:textSize="13sp" 
 
      tools:layout_editor_absoluteX="0dp" 
 
      tools:layout_editor_absoluteY="0dp" /> 
 

 
     <TextView 
 
      android:id="@+id/txtRecipeCalorie" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_below="@+id/txtRecipeName" 
 
      android:layout_toRightOf="@+id/txtCalorie" 
 
      android:text="999" 
 
      android:textColor="@android:color/black" 
 
      android:textSize="13sp" 
 
      tools:layout_editor_absoluteX="0dp" 
 
      tools:layout_editor_absoluteY="0dp" /> 
 

 
     <TextView 
 
      android:id="@+id/txtKcal" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_below="@+id/txtRecipeName" 
 
      android:layout_marginLeft="5dp" 
 
      android:layout_toRightOf="@+id/txtRecipeCalorie" 
 
      android:text="kcal" 
 
      android:textColor="@android:color/black" 
 
      android:textSize="13sp" 
 
      android:textStyle="bold" 
 
      tools:layout_editor_absoluteX="0dp" 
 
      tools:layout_editor_absoluteY="0dp" /> 
 

 

 
    </RelativeLayout> 
 

 
</android.support.v7.widget.CardView>

順便說一句,因爲我們都在這裏,可以有人告訴我如何提高各卡之間的空間?我目前在CardView裏面的相對佈局裏面使用了marginLeft,但這絕對不是。

在此先感謝您的回答!

+0

將「ImageView」的大小設置爲「CardView」大小。對於兩個項目之間的設置空間,請檢查這個https://stackoverflow.com/questions/24618829/how-to-add-dividers-and-spaces-between-items-in-recyclerview – Ninja

+0

你的'CardView'對我來說看起來不錯。你是如何加載圖像?當我在示例項目中過濾XML時,照片會被裁剪並看起來像您想要的圖像。順便說一句,你有兩個XML文件切換你的問題。另外,你應該用'match_parent'來代替'fill_parent'(不建議使用)。 – Cheticamp

回答

0

設置一個最大寬度到您的ImageView

 <ImageView 
      android:id="@+id/recipeThumbnail" 
      android:maxWidth="150dp"     //Change to suit 
      android:scaleType="centerCrop"   //Trims accordingly 
      android:layout_width="match_parent" 
      android:layout_height="195dp" 
      android:contentDescription="" 
      android:src="@drawable/placeholder" 
      android:scaleType="centerCrop" 
      tools:layout_editor_absoluteX="0dp" 
      tools:layout_editor_absoluteY="0dp" /> 

順便說一句,因爲我們都在這裏,可以有人告訴我如何增加 各卡之間的空間?我目前正在使用marginLeft裏面的CardView裏面的相對佈局 ,但這絕對不是 的方式。

結賬this回答。

相關問題