【问题标题】:Diamond shape xml background for android viewandroid视图的菱形xml背景
【发布时间】:2017-04-26 06:10:37
【问题描述】:

大家好,请建议我如何创建如下图所示的 android xml drawable?

现在我使用下面的代码

<?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="@color/colorAccent" />
                <solid android:color="@color/colorAccent" />
            </shape>
        </rotate>
    </item>
</layer-list>

但它的结果是三角形,但我需要上面的形状。

【问题讨论】:

标签: android xml android-drawable shape


【解决方案1】:

检查一下,

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:left="43dp"
    android:top="43dp"
    android:right="43dp"
    android:bottom="43dp">
    <rotate android:fromDegrees="45">
        <shape>
            <size
                android:width="200dp"
                android:height="200dp" />
            <gradient
                android:startColor="#0fdcc9"
                android:endColor="#0fdcc9" />
            <stroke
                android:width="5dp"
                android:color="#2DACA0" />
        </shape>
    </rotate>
</item>
</layer-list>

【讨论】:

  • 完美答案。只需将startColorendColor 更改为#66BF9E 值并在笔划color 中更改为#318264 值。
  • 这是我使用的解决方案,但您不需要 ; 可以。
  • @greeble31 &lt;solid&gt; 标签适用于单一的纯色,如果我们想使用多个,我们可以使用&lt;gradient&gt;。在这种情况下,我们可以使用 &lt;solid&gt; 而不是 两者都可以。
【解决方案2】:

这是我的钻石形状的可绘制文件ic_diamond.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="372.7dp"
android:height="367.4dp"
android:viewportWidth="372.7"
android:viewportHeight="367.4">

<path
    android:fillColor="#50AE89"
    android:strokeColor="#266D4F"
    android:strokeWidth="5"
    android:strokeMiterLimit="10"
    android:pathData="M188.706,51.8702 L318.105,181.269 L183.968,315.406 L54.5691,186.007
                   L188.706,51.8702 Z" />
</vector>

我将其包含在简单视图中:

<View
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_centerInParent="true"
    android:background="@drawable/ic_diamond"
    />

结果如下:

【讨论】:

  • 你能分享一下你是如何生成这个矢量图像的吗?我尝试使用 android studio 但仍然面临问题
  • 我只是创建了 svg 并将其转换为矢量。 :D
【解决方案3】:

我修复了本教程 here

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="43dp"
        android:left="43dp"
        android:right="43dp"
        android:state_pressed="true"
        android:top="43dp">
        <rotate android:fromDegrees="45">
            <shape>
                <size
                    android:width="200dp"
                    android:height="200dp" />
                <gradient
                    android:endColor="@color/colorPrimaryDark"
                    android:startColor="@color/colorPrimaryDark" />

                <stroke
                    android:width="5dp"
                    android:color="@color/colorPrimary" />
                <corners android:radius="7dp" />
                <padding
                    android:bottom="10dp"
                    android:left="10dp"
                    android:right="10dp"
                    android:top="10dp" />
            </shape>
        </rotate>
    </item>
</layer-list>

【讨论】:

    【解决方案4】:

    这是一个可绘制资源,它创建一个 200dp 的正方形并将其旋转 45° 以形成菱形:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="50dp"
            android:left="50dp"
            android:right="50dp"
            android:top="50dp">
            <rotate android:fromDegrees="45">
                <shape>
                    <size
                        android:width="200dp"
                        android:height="200dp"/>
                    <stroke
                        android:width="10dp"
                        android:color="#358165"/>
                    <solid android:color="#69BE9E"/>
                </shape>
            </rotate>
        </item>
    </layer-list>
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    相关资源
    最近更新 更多