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,但这绝对不是。
在此先感谢您的回答!
将“ImageView”的大小设置为“CardView”大小。对于两个项目之间的设置空间,请检查这个https://stackoverflow.com/questions/24618829/how-to-add-dividers-and-spaces-between-items-in-recyclerview – Ninja
你的'CardView'对我来说看起来不错。你是如何加载图像?当我在示例项目中过滤XML时,照片会被裁剪并看起来像您想要的图像。顺便说一句,你有两个XML文件切换你的问题。另外,你应该用'match_parent'来代替'fill_parent'(不建议使用)。 – Cheticamp