请CardView的ImageView的合适宽度 [英] Make ImageView fit width of CardView
问题描述
我有一个 CardView
带圆角的,我想有一个的ImageView
上方像所示例如来自下面的材料设计方针。
I have a CardView
with rounded corners, I want to have an ImageView
at the top like shown in the example taken from the material design guidelines below.
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp">
<!-- ... -->
</android.support.v7.widget.CardView>
然后在 CardView
我有这样的的ImageView
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="@drawable/default_cover" />
如果我有 card_view:cardCornerRadius
设置为 0dp
则 ImageView的
适合的卡就像我希望它。
If I have the card_view:cardCornerRadius
set to 0dp
then the ImageView
fits the card like how I want it to.
不过,材料设计方针状态的卡应该有圆角,而不是方角。
However, the material design guidelines state that cards should have rounded corners, and not square corners.
我的问题是,当我设置了 card_view:cardCornerRadius
来的东西以外 0dp
,例如: 4DP
,然后会发生以下情况:
The problem I have is when I set the card_view:cardCornerRadius
to something other than 0dp
, e.g. 4dp
, then the following happens:
可以看出,在的ImageView
不适合的 CardView
。
As can be seen, the ImageView
does not fit into the CardView
.
我的问题是,我怎样才能使这个的ImageView
适合的布局 CardView
时,它已经四舍五入角落。
My question is, how can I make this ImageView
fit to the layout of the CardView
when it has rounded corners.
推荐答案
您需要做两件事情:
1)呼叫设置preventCornerOverlap(假)
您CardView。
1) Call setPreventCornerOverlap(false)
on your CardView.
2)将 圆形的的ImageView内CardView
2) Put rounded Imageview inside CardView
有关舍入您的ImageView的,我有同样的问题,所以我做了一个库,你可以将的不同的半径上的每个角落的。有一个良好的库(vinc3m1的RoundedImageView),支持上ImageView的圆角,但只支持在每个角部相同的半径。但我想它是圆形的的只有的左上角和右上角。
About rounding your imageview, I had the same problem so I made a library that you can set different radii on each corners. There is one good library(vinc3m1’s RoundedImageView) that supports rounded corners on ImageView, but it only supports the same radii on every corners. But I wanted it to be rounded only top left and top right corners.
最后我得到的结果我想要的东西象下面。
Finally I got the result what I wanted like below.
<一个href="https://github.com/pungrue26/SelectableRoundedImageView">https://github.com/pungrue26/SelectableRoundedImageView
这篇关于请CardView的ImageView的合适宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!