【问题标题】:Android CardView Doesn't round corners when using app:cardCornerRadius="5dp"Android CardView 使用 app:cardCornerRadius="5dp" 时不圆角
【发布时间】:2020-11-14 09:38:54
【问题描述】:

我有一组要显示的图像,我在卡片视图中使用 smarteists 图像滑块,现在我想绕过这个卡片视图的角落。我在 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 android xml android-studio android-cardview


    【解决方案1】:

    使用 Java 代码在您的 NoLinkCardview 上调用 setClipToOutline(true)

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

    【讨论】:

    • 试过了,还是一样的问题:还是直角。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 2021-05-04
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多