MaterialCardView的不同角半径值

时间:2019-02-14 10:55:06

标签: android android-layout material-components material-components-android

对于MaterialCardView的每个角半径,是否可以具有不同的值?如果是这样怎么办?

我尝试了以下代码,但似乎没有任何作用

    float radius = getContext().getResources().getDimension(R.dimen.default_corner_radius);
    ShapePathModel leftShapePathModel = new ShapePathModel();
    leftShapePathModel.setTopLeftCorner(new RoundedCornerTreatment(radius));
    leftShapePathModel.setTopRightCorner(new RoundedCornerTreatment(radius));
    MaterialShapeDrawable bg = new MaterialShapeDrawable(leftShapePathModel);
    container.setBackground(bg);

容器在哪里

@BindView(R.id.container) MaterialCardView container;

4 个答案:

答案 0 :(得分:1)

我认为您应该可以在ShapeAppearanceModel shape = ((MaterialShapeDrawable)container.getBackground()).getShapeAppearanceModel()上致电MaterialCardView。从那里,您可以调用setTopLeftCorner()或其他方法以不同的值设置边角处理。设置拐角后,您可能需要致电container.invalidate()

答案 1 :(得分:0)

我最初的解决方案是正确的,但是缺少一行:

float radius = getContext().getResources().getDimension(R.dimen.default_corner_radius);
ShapePathModel leftShapePathModel = new ShapePathModel();
leftShapePathModel.setTopLeftCorner(new RoundedCornerTreatment(radius));
leftShapePathModel.setTopRightCorner(new RoundedCornerTreatment(radius));
MaterialShapeDrawable bg = new MaterialShapeDrawable(leftShapePathModel);
container.setBackground(bg);

如果添加

container.invalidate()
正如卡梅伦在上面建议的那样,它似乎起作用。

答案 2 :(得分:0)

您可以使用自定义样式 library(validate) # quantile calculation should be applied only to subset of species setosa v <- validator(if (Species=="setosa") quantile(Petal.Length, 0.75) > Petal.Length ) # confront data set and validation rule cf <- confront(iris, v) summary(cf) # name items passes fails nNA error warning expression # 1 V1 150 150 0 0 FALSE FALSE !(Species == "setosa") | (quantile(Petal.Length, 0.75) > Petal.Length) # All items pass because the quantile for the total data set is calculated, where setosa is the smallest # quantile(iris[iris$Species=="virginica", "Petal.Length"], 0.75) # 1.575 # max(iris[iris$Species=="setosa", "Petal.Length"]) # 1.9 属性。

shapeAppearanceOverlay

或者您可以使用类似的方法将自定义ShapeAppearanceModel应用于卡片的角落:

  <style name="MyCardView" parent="@style/Widget.MaterialComponents.CardView">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MaterialCardView.Cut</item>
  </style>


  <style name="ShapeAppearanceOverlay.MaterialCardView.Cut" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopRight">8dp</item>
    <item name="cornerSizeTopLeft">8dp</item>
    <item name="cornerSizeBottomRight">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>
  </style>

enter image description here

答案 3 :(得分:0)

也可以尝试

     <style name="TopCornerCardview" parent="Widget.MaterialComponents.CardView">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSizeTopRight">@dimen/dp25</item>
        <item name="cornerSizeTopLeft">@dimen/dp25</item>
        <item name="cornerSizeBottomRight">0dp</item>
        <item name="cornerSizeBottomLeft">0dp</item>
        <item name="contentPadding">0dp</item>
    </style>

    <com.google.android.material.card.MaterialCardView
            style="@style/TopCornerCardview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/TopCornerCardview"
            app:cardBackgroundColor="@color/colorAccent"
            app:cardUseCompatPadding="true">

>
<!--views here-->
</com.google.android.material.card.MaterialCardView>