如何在 Android 中调整图像大小以适应 CardView 中的固定大小?
How to resize images to fit a fixed size inside a CardView in Android?
我有几个 CardView,每个 CardView 的左边都有一个 ImageView。
每个 ImageView 都有一个 layout_weight 以使其适合每个 CardView 的相同宽度和固定高度(在本例中为 80dp)。
当图像水平时,它们看起来不错,例如 1、2 和 5 .但是当它们是垂直的时,它们不适合 3 和 4.
我已经尝试过:直接在 ImageViews 上声明 layout_weight/在 LinearLayout 中移动 ImageViews 并在其上声明 layout_weight(实际代码)/按钮而不是 ImageViews/使用 adjustViewBounds 和每个 scaleType。 None 有效。
这是我为每个 CardView 编写的实际代码,如有任何帮助,我将不胜感激!提前谢谢你:)
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="3dp"
android:layout_marginTop="3dp"
android:layout_marginLeft="7dp"
android:layout_marginStart="7dp"
android:layout_marginRight="7dp"
android:layout_marginEnd="7dp">
<LinearLayout android:id="@+id/cardview_inner_linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout android:id="@+id/imageview_linearlayout"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="7">
<ImageView android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/halloween1" />
</LinearLayout>
<LinearLayout android:id="@+id/whitearea_linearlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
// White area to the right of the image
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
您可以尝试更改图片的缩放类型
我强烈建议开始使用百分比而不是权重
https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html
现在您可以使用以下布局:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="3dp"
app:cardElevation="3dp">
<LinearLayout
android:id="@+id/cardview_inner_linearlayout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/imageview_linearlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@drawable/halloween1"/>
</LinearLayout>
<LinearLayout
android:id="@+id/whitearea_linearlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
在 ImageView 属性中使用 android:scaleType="centerCrop"
。
我有几个 CardView,每个 CardView 的左边都有一个 ImageView。
每个 ImageView 都有一个 layout_weight 以使其适合每个 CardView 的相同宽度和固定高度(在本例中为 80dp)。
当图像水平时,它们看起来不错,例如 1、2 和 5 .但是当它们是垂直的时,它们不适合 3 和 4.
我已经尝试过:直接在 ImageViews 上声明 layout_weight/在 LinearLayout 中移动 ImageViews 并在其上声明 layout_weight(实际代码)/按钮而不是 ImageViews/使用 adjustViewBounds 和每个 scaleType。 None 有效。
这是我为每个 CardView 编写的实际代码,如有任何帮助,我将不胜感激!提前谢谢你:)
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="3dp"
android:layout_marginTop="3dp"
android:layout_marginLeft="7dp"
android:layout_marginStart="7dp"
android:layout_marginRight="7dp"
android:layout_marginEnd="7dp">
<LinearLayout android:id="@+id/cardview_inner_linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout android:id="@+id/imageview_linearlayout"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="7">
<ImageView android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/halloween1" />
</LinearLayout>
<LinearLayout android:id="@+id/whitearea_linearlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
// White area to the right of the image
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
您可以尝试更改图片的缩放类型
我强烈建议开始使用百分比而不是权重 https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html
现在您可以使用以下布局:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="3dp"
app:cardElevation="3dp">
<LinearLayout
android:id="@+id/cardview_inner_linearlayout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/imageview_linearlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@drawable/halloween1"/>
</LinearLayout>
<LinearLayout
android:id="@+id/whitearea_linearlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
在 ImageView 属性中使用 android:scaleType="centerCrop"
。