Android 安卓:如何制作三角形布局

Android 安卓:如何制作三角形布局,android,android-layout,android-shapedrawable,Android,Android Layout,Android Shapedrawable,我想为谷歌地图定制信息窗口 我能做到,但我不能做三角波纹管布局。 我可以添加图像,但布局有阴影的外线。 有人建议我怎么做吗。 如何制作红色区域内的部分。正如你们所见,外部布局有阴影 您可以使用图层列表。在底部创建一个三角形,然后创建一个矩形作为下一层,并给出与三角形高度匹配的底部边距 下面是一个制作三角形的示例 您可以使用背景作为自定义布局。使用背景可以实现您的目标。是一个很好的开始链接。尝试以下内容: 对于三角形: <?xml version="1.0" encoding="ut

我想为谷歌地图定制信息窗口 我能做到,但我不能做三角波纹管布局。 我可以添加图像,但布局有阴影的外线。 有人建议我怎么做吗。 如何制作红色区域内的部分。正如你们所见,外部布局有阴影


您可以使用图层列表。在底部创建一个三角形,然后创建一个矩形作为下一层,并给出与三角形高度匹配的底部边距

下面是一个制作三角形的示例

您可以使用背景作为自定义布局。

使用背景可以实现您的目标。是一个很好的开始链接。

尝试以下内容:

对于三角形:

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
            android:fromDegrees="45"
            android:pivotX="-40%"
            android:pivotY="87%"
            android:toDegrees="45">
            <shape android:shape="rectangle">
                <stroke
                    android:width="10dp"
                    android:color="@android:color/transparent" />
                <solid android:color="#000000" />
            </shape>
        </rotate>
    </item>
</layer-list>

对于矩形:

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#B2E3FA" />
        </shape>
    </item>
</layer-list>

在布局中使用两种xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/rlv1"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:background="@drawable/rectringle" />
    <RelativeLayout
        android:id="@+id/rlv2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_below="@+id/rlv1"
        android:layout_marginLeft="30dp"
        android:background="@drawable/tringle"
        android:rotation="180" />
</LinearLayout>

您可以使用
创建此自定义形状。下面是一个工作示例。将
custom\u triangal\u shape.xml
放入您的
res/drawable
文件夹中

自定义三角形状.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Transparent Rectangle -->
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="300dp"
                android:height="80dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <!-- Colored Rectangle -->
    <item
        android:bottom="20dp">
        <shape android:shape="rectangle">
            <corners
                android:radius="4dp">

            </corners>
            <size
                android:width="300dp"
                android:height="80dp" />
            <solid android:color="#FFFFFF" />
        </shape>
    </item>

    <!-- Bottom Triangle -->
    <item
        android:left="90dp"
        android:right="110dp"
        android:top="0dp"
        android:bottom="30dp">
        <rotate android:fromDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="#FFFFFF" />
            </shape>
        </rotate>
    </item>

</layer-list>

使用:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_triangular_shape">

</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/backgroundWhite"
        app:cardElevation="4dp"
        app:cardCornerRadius="8dp">

        //Content layout here          

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

    <com.google.android.material.card.MaterialCardView
        style="@style/PointerCardViewStyle"
        android:layout_width="20dp"
        android:layout_height="10dp"
        app:cardBackgroundColor="@color/whiteColor"
        android:layout_gravity="center"
        app:cardElevation="4dp" />
</LinearLayout>
<style name="PointerCardViewStyle" parent="Widget.MaterialComponents.CardView">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlayPointerCardView</item>
</style>

<style name="ShapeAppearanceOverlayPointerCardView" parent="@style/Widget.MaterialComponents.CardView">
    <item name="cornerFamily">cut</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomRight">10dp</item>
    <item name="cornerSizeBottomLeft">10dp</item>
</style>

输出:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_triangular_shape">

</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/backgroundWhite"
        app:cardElevation="4dp"
        app:cardCornerRadius="8dp">

        //Content layout here          

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

    <com.google.android.material.card.MaterialCardView
        style="@style/PointerCardViewStyle"
        android:layout_width="20dp"
        android:layout_height="10dp"
        app:cardBackgroundColor="@color/whiteColor"
        android:layout_gravity="center"
        app:cardElevation="4dp" />
</LinearLayout>
<style name="PointerCardViewStyle" parent="Widget.MaterialComponents.CardView">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlayPointerCardView</item>
</style>

<style name="ShapeAppearanceOverlayPointerCardView" parent="@style/Widget.MaterialComponents.CardView">
    <item name="cornerFamily">cut</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomRight">10dp</item>
    <item name="cornerSizeBottomLeft">10dp</item>
</style>


希望这会有帮助~

您可以使用材质组件库创建自定义组件。
该库提供创建给定大小的三角形处理方式的,相对于形状向内或向外的

您可以使用以下内容:

TriangleEdgeTreatment triangleEdgeTreatment = new TriangleEdgeTreatment(radius,false);

LinearLayout linearLayout= findViewById(R.id.xxxx);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
    .toBuilder()
    .setBottomEdge(triangleEdgeTreatment)
    .build();

MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(linearLayout,shapeDrawable);

此外,对于边处理,父视图必须禁用子视图的剪裁
在xml中设置android:clipChildren=“false”。
clipToPadding
如果父级上有任何填充可能与阴影相交,则可能也需要设置为
false

这可以通过使用
MaterialCardView
和来实现

依赖项:(使用)

布局:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_triangular_shape">

</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/backgroundWhite"
        app:cardElevation="4dp"
        app:cardCornerRadius="8dp">

        //Content layout here          

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

    <com.google.android.material.card.MaterialCardView
        style="@style/PointerCardViewStyle"
        android:layout_width="20dp"
        android:layout_height="10dp"
        app:cardBackgroundColor="@color/whiteColor"
        android:layout_gravity="center"
        app:cardElevation="4dp" />
</LinearLayout>
<style name="PointerCardViewStyle" parent="Widget.MaterialComponents.CardView">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlayPointerCardView</item>
</style>

<style name="ShapeAppearanceOverlayPointerCardView" parent="@style/Widget.MaterialComponents.CardView">
    <item name="cornerFamily">cut</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomRight">10dp</item>
    <item name="cornerSizeBottomLeft">10dp</item>
</style>

//此处的内容布局
风格:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_triangular_shape">

</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/backgroundWhite"
        app:cardElevation="4dp"
        app:cardCornerRadius="8dp">

        //Content layout here          

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

    <com.google.android.material.card.MaterialCardView
        style="@style/PointerCardViewStyle"
        android:layout_width="20dp"
        android:layout_height="10dp"
        app:cardBackgroundColor="@color/whiteColor"
        android:layout_gravity="center"
        app:cardElevation="4dp" />
</LinearLayout>
<style name="PointerCardViewStyle" parent="Widget.MaterialComponents.CardView">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlayPointerCardView</item>
</style>

<style name="ShapeAppearanceOverlayPointerCardView" parent="@style/Widget.MaterialComponents.CardView">
    <item name="cornerFamily">cut</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomRight">10dp</item>
    <item name="cornerSizeBottomLeft">10dp</item>
</style>

@样式/形状PearanceOverlyPointerCardView
切
0dp
0dp
10dp
10dp
结果:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_triangular_shape">

</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/backgroundWhite"
        app:cardElevation="4dp"
        app:cardCornerRadius="8dp">

        //Content layout here          

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

    <com.google.android.material.card.MaterialCardView
        style="@style/PointerCardViewStyle"
        android:layout_width="20dp"
        android:layout_height="10dp"
        app:cardBackgroundColor="@color/whiteColor"
        android:layout_gravity="center"
        app:cardElevation="4dp" />
</LinearLayout>
<style name="PointerCardViewStyle" parent="Widget.MaterialComponents.CardView">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlayPointerCardView</item>
</style>

<style name="ShapeAppearanceOverlayPointerCardView" parent="@style/Widget.MaterialComponents.CardView">
    <item name="cornerFamily">cut</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomRight">10dp</item>
    <item name="cornerSizeBottomLeft">10dp</item>
</style>

我不想知道如何实现自定义信息窗口。我只想知道如何使布局看起来与给定的图像相似。请检查此链接此答案与我的问题非常接近,但我提到的缺失也有阴影效果。若我给它阴影,那个么内矩形也有我不想要的阴影。我只是想展示一下外线。请推荐我。如果你有任何东西。对于阴影,你必须添加另一个带顶部填充的2个矩形。你能在你的代码中进行更改吗。因为我对安卓非常陌生,所以我已经考虑过了。顺便说一句,我已经实现了自己,但如果你有什么新的,请更新博客的窗口大小根据内部内容。在这里,您正在将一个大小硬编码到图层列表中。不完全一样。我支持你们的努力,但有很多东西你们忽略了,比如外线的阴影。那样的话,你的方法就行不通了。