Tud*_* S. 10 android android-layout android-cardview 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);
Run Code Online (Sandbox Code Playgroud)
容器在哪里
@BindView(R.id.container) MaterialCardView container;
Run Code Online (Sandbox Code Playgroud)
Gab*_*tti 15
您可以使用自定义样式和shapeAppearanceOverlay属性。
<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>
Run Code Online (Sandbox Code Playgroud)
或者您可以使用以下内容将自定义ShapeAppearanceModel应用于卡片的角落:
float radius = getResources().getDimension(R.dimen.my_corner_radius);
cardView.setShapeAppearanceModel(
cardView.getShapeAppearanceModel()
.toBuilder()
.setTopLeftCorner(CornerFamily.ROUNDED,radius)
.setTopRightCorner(CornerFamily.ROUNDED,radius)
.setBottomRightCornerSize(0)
.setBottomLeftCornerSize(0)
.build());
Run Code Online (Sandbox Code Playgroud)
也试试这个
<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>
Run Code Online (Sandbox Code Playgroud)
我最初的解决方案是正确的,但缺少一行:
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);
Run Code Online (Sandbox Code Playgroud)
如果你添加
container.invalidate()
Run Code Online (Sandbox Code Playgroud)
正如卡梅伦上面所建议的,它似乎有效。
| 归档时间: |
|
| 查看次数: |
5273 次 |
| 最近记录: |