有什么办法可以改变Material设计CardView的特定角的半径
Is there any way to change the radius of specific corner of Material design CardView
我在我的 android 应用程序中使用 material 设计,通常我使用 app:cardCornerRadius 来更改卡片角的半径,但是这将改变卡片的所有四个角。我想更改卡片的任何特定角。我不想只使用 drawable 来改变角落。有没有办法通过 xml 或以编程方式。
<com.google.android.material.card.MaterialCardView
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorWhite"
app:cardCornerRadius="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="16dp" android:layout_marginEnd="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent">
我不太确定 Material 设计卡片视图是否具有该功能。但我不认为默认的 cardview 有那种功能,所以代表 material cardview 你可以使用
这只是我的意见,也许还有另一个好的解决方案。
我有一个解决方案但它需要 v21 Lollipop 及更高版本。
您可以按如下方式以编程方式执行此操作:
在你的activityclass有public static int radius = 20;
在你的 Oncreate()
;
CardView card = findViewById(R.id.my_card);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
makeCustomOutline(card);
}
然后定义函数如下:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void makeCustomOutline(CardView card){
card.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(),
(view.getHeight() + radius), (float)radius );
}
});
card.setClipToOutline(true);
}
通过使用 setRoundRect()
方法,您可以控制 cardview 的哪个角获得半径,哪个角没有。上面的代码只会使顶角变圆,底角不会变圆。
参考文献:
使用 Material 组件的 1.1.0 版,您可以使用 shapeAppearanceOverlay
属性 customize the shape 您的组件。
您可以在视图或样式中使用它。
<com.google.android.material.card.MaterialCardView
.....
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MaterialCardView.Cut">
然后定义你喜欢的形状:
<style name="ShapeAppearanceOverlay.MaterialCardView.Cut" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">0dp</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeBottomRight">16dp</item>
<item name="cornerSizeBottomLeft">16dp</item>
</style>
使用代码,您可以使用以下内容将自定义 ShapeAppearanceModel
应用到卡片的一角:
float radius = getResources().getDimension(R.dimen.my_corner_radius);
cardView.setShapeAppearanceModel(
cardView.getShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.setTopRightCornerSize(0)
.build());
我在我的 android 应用程序中使用 material 设计,通常我使用 app:cardCornerRadius 来更改卡片角的半径,但是这将改变卡片的所有四个角。我想更改卡片的任何特定角。我不想只使用 drawable 来改变角落。有没有办法通过 xml 或以编程方式。
<com.google.android.material.card.MaterialCardView
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorWhite"
app:cardCornerRadius="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="16dp" android:layout_marginEnd="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent">
我不太确定 Material 设计卡片视图是否具有该功能。但我不认为默认的 cardview 有那种功能,所以代表 material cardview 你可以使用
这只是我的意见,也许还有另一个好的解决方案。
我有一个解决方案但它需要 v21 Lollipop 及更高版本。 您可以按如下方式以编程方式执行此操作:
在你的activityclass有public static int radius = 20;
在你的 Oncreate()
;
CardView card = findViewById(R.id.my_card);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
makeCustomOutline(card);
}
然后定义函数如下:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void makeCustomOutline(CardView card){
card.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(0, 0, view.getWidth(),
(view.getHeight() + radius), (float)radius );
}
});
card.setClipToOutline(true);
}
通过使用 setRoundRect()
方法,您可以控制 cardview 的哪个角获得半径,哪个角没有。上面的代码只会使顶角变圆,底角不会变圆。
参考文献:
使用 Material 组件的 1.1.0 版,您可以使用 shapeAppearanceOverlay
属性 customize the shape 您的组件。
您可以在视图或样式中使用它。
<com.google.android.material.card.MaterialCardView
.....
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MaterialCardView.Cut">
然后定义你喜欢的形状:
<style name="ShapeAppearanceOverlay.MaterialCardView.Cut" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">0dp</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeBottomRight">16dp</item>
<item name="cornerSizeBottomLeft">16dp</item>
</style>
使用代码,您可以使用以下内容将自定义 ShapeAppearanceModel
应用到卡片的一角:
float radius = getResources().getDimension(R.dimen.my_corner_radius);
cardView.setShapeAppearanceModel(
cardView.getShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.setTopRightCornerSize(0)
.build());