Android CardView 在使用 app:cardCornerRadius="5dp" 时不圆角

Android CardView Doesn't round corners when using app:cardCornerRadius="5dp"

我有一组要显示的图像,我在 cardview 中使用了 smarteists 图像滑块,现在我想圆这个 cardview 的角。我在 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));

在 Java 代码中对您的 NoLinkCardview 调用 setClipToOutline(true)

CardView card = findViewById(R.id.NoLinkCardview)
card.setClipToOutline(true)

我能够通过在 Sliderview 中使用填充来解决这个问题。我添加了这些属性

            android:paddingVertical="5dp"
            android:paddingHorizontal="5dp"

添加到我的 XML 文件中,这些在为我的 CardView 设置的背景颜色中围绕我的图片创建了一个小框架。