为MaterialCardView设置 "外部 "底边不能如愿以偿。

问题描述 投票:2回答:1

我想建立一个 tooltip 像下面的图片一样,带有阴影。

enter image description here

我可以通过使用Image as来做到这一点 background. 但我无法将阴影应用于它。所以,经过搜索,我发现 本文它使用 MaterialCardView 并应用 MaterialShapeDrawable 以此作为 background.

我已经尝试了以下代码。

    val shapeDrawable = MaterialShapeDrawable()
    val shapeAppearanceModel =
        ShapeAppearanceModel.builder().setBottomEdge(TriangleEdgeTreatment(20f, false)).build()
    shapeDrawable.shapeAppearanceModel = shapeAppearanceModel
    materialDialog.background = shapeDrawable

XML文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="@dimen/width_custom_marker"
    android:layout_height="100dp"
    android:padding="16dp"
    tools:ignore="Overdraw">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/materialCard"
        android:layout_width="@dimen/width_custom_marker"
        android:layout_height="50dp" />
</RelativeLayout>

我的问题 是,当设置 inside 旗帜与 false 该对象的 TriangleEdgeTreatment(30f, true) 并没有达到预期的效果。然而,将标志设置为 true 正常工作。

TriangleEdgeTreatment(30f, false)

enter image description here

TriangleEdgeTreatment(30f, true)

enter image description here

先谢谢你

android tooltip material-components-android materialcardview
1个回答
1
投票

我可以用以下方法来实现。

  <LinearLayout
      android:clipChildren="false"
      android:clipToPadding="false"
      android:padding="16dp"
      ..>

        <com.google.android.material.card.MaterialCardView
         ../>

并应用 TriangleEdgeTreatment:

float size = getResources().getDimension(R.dimen.triangle_size); //16dp
TriangleEdgeTreatment triangleEdgeTreatment = new TriangleEdgeTreatment(size,false);

MaterialCardView cardView = findViewById(R.id.card);
cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel()
    .toBuilder()
    .setBottomEdge(triangleEdgeTreatment)
    .build());

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.