英文:
Android CardView Doesn't round corners when using app:cardCornerRadius="5dp"
问题
我有一组图像数组,我想要展示,并且我在卡片视图内使用了SmartImageView图像滑块,现在我想要将此卡片视图的角落变成圆角。我在 XML 文件中使用了 app:cardCornerRadius="5dp"
,但没有效果,当我在 Java 文件中尝试使用 .setRadius(5)
也没有任何效果。奇怪的是,在我使用图像滑块之前,我使用了一个 ViewPager,它的角落可以很好地变成圆角。我不知道我做错了什么,如果有人发现了我的错误,我将非常感激。
XML 代码:
<androidx.cardview.widget.CardView
android:id="@+id/NoLinkCardview"
android:layout_width="392dp"
android:layout_height="258dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.smarteist.autoimageslider.SliderView
android:id="@+id/NoLinkImageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:sliderIndicatorGravity="center_horizontal|bottom"
app:sliderIndicatorMargin="15dp"
app:sliderIndicatorOrientation="horizontal"
app:sliderIndicatorPadding="3dp"
app:sliderIndicatorRadius="1.8dp"/>
</androidx.cardview.widget.CardView>
Java 代码:
SliderView sliderView = findViewById(R.id.NoLinkImageSlider);
SliderAdapter adapter = new SliderAdapter(this);
List<SliderItem> currentSliderItems = new ArrayList<>();
currentSliderItems.clear();
assert currentPointOfInterest != null;
for(int j = 0; j<currentPointOfInterest.getAmountOfPictures(); j++){
SliderItem sliderItem = new SliderItem();
sliderItem.setImageUrl(Objects.requireNonNull(picturesMap.get(currentPointOfInterest.getID()))[j]);
currentSliderItems.add(sliderItem);
}
adapter.renewItems(currentSliderItems);
sliderView.setSliderAdapter(adapter);
sliderView.setInfiniteAdapterEnabled(true);
sliderView.setIndicatorAnimation(IndicatorAnimationType.COLOR);
sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
sliderView.setIndicatorSelectedColor(getColor(R.color.colorAccent));
sliderView.setIndicatorUnselectedColor(getColor(R.color.colorAccentTransparent));
英文:
I have an array of images that I want to display and I use smarteists image slider inside a cardview and now I want to round the corners of this cardview. I use app:cardCornerRadius="5dp" in the xml file and it doesn't work, when I try .setRadius(5) in the java file it also doesn't do anything. The strange thing is, before I used the image slider, I used a ViewPager and it rounded the corners just fine. I have no idea what I'm doing wrong so if anybody finds my mistake it would be greatly appreciated.
XML code:
<androidx.cardview.widget.CardView
android:id="@+id/NoLinkCardview"
android:layout_width="392dp"
android:layout_height="258dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.smarteist.autoimageslider.SliderView
android:id="@+id/NoLinkImageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:sliderIndicatorGravity="center_horizontal|bottom"
app:sliderIndicatorMargin="15dp"
app:sliderIndicatorOrientation="horizontal"
app:sliderIndicatorPadding="3dp"
app:sliderIndicatorRadius="1.8dp"
/>
</androidx.cardview.widget.CardView>
Java Code:
SliderView sliderView = findViewById(R.id.NoLinkImageSlider);
SliderAdapter adapter = new SliderAdapter(this);
List<SliderItem> currentSliderItems = new ArrayList<>();
currentSliderItems.clear();
assert currentPointOfInterest != null;
for(int j = 0; j<currentPointOfInterest.getAmountOfPictures(); j++){
SliderItem sliderItem = new SliderItem();
sliderItem.setImageUrl(Objects.requireNonNull(picturesMap.get(currentPointOfInterest.getID()))[j]);
currentSliderItems.add(sliderItem);
}
adapter.renewItems(currentSliderItems);
sliderView.setSliderAdapter(adapter);
sliderView.setInfiniteAdapterEnabled(true);
sliderView.setIndicatorAnimation(IndicatorAnimationType.COLOR);
sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
sliderView.setIndicatorSelectedColor(getColor(R.color.colorAccent));
sliderView.setIndicatorUnselectedColor(getColor(R.color.colorAccentTransparent));
答案1
得分: 1
在Java代码中,在您的NoLinkCardview
上调用setClipToOutline(true)
。
CardView card = findViewById(R.id.NoLinkCardview);
card.setClipToOutline(true);
英文:
Call setClipToOutline(true)
on your NoLinkCardview
in Java code.
CardView card = findViewById(R.id.NoLinkCardview)
card.setClipToOutline(true)
答案2
得分: 0
我能够通过在滑动视图(Sliderview)中使用填充来解决这个问题。我在我的 XML 文件中添加了以下属性:
android:paddingVertical="5dp"
android:paddingHorizontal="5dp"
这样在我的 CardView 背景颜色所设定的背景色中,为我的图片创建了一个小框架。
英文:
I was able to fix this issue by using padding in the Sliderview. I added these attributes
android:paddingVertical="5dp"
android:paddingHorizontal="5dp"
to my XML file and these created a little frame around my pictures in the backgroundcolour that was set for my CardView.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论